From d0e1f457083bed6e0cb7b110495cd0e4c93037c9 Mon Sep 17 00:00:00 2001 From: "Daniel G. Taylor" Date: Tue, 22 Mar 2022 09:33:47 -0700 Subject: [PATCH] fix: better handling of zero values; no panic for generic types --- context.go | 4 ++-- graphql_model.go | 4 +++- schema/schema.go | 6 +++++- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/context.go b/context.go index d9e5ce62..a6ece894 100644 --- a/context.go +++ b/context.go @@ -239,13 +239,13 @@ func shallowStructToMap(v reflect.Value, result map[string]interface{}) { if name == "-" { continue } - if len(parts) == 2 && parts[1] == "omitempty" && v.Field(i).IsZero() { + if len(parts) > 1 && parts[1] == "omitempty" { vf := v.Field(i) zero := vf.IsZero() if vf.Kind() == reflect.Slice || vf.Kind() == reflect.Map { // Special case: omit if they have no items in them to match the // JSON encoder. - zero = vf.Len() > 0 + zero = vf.Len() == 0 } if zero { continue diff --git a/graphql_model.go b/graphql_model.go index 5d5e50a9..8136b18c 100644 --- a/graphql_model.go +++ b/graphql_model.go @@ -32,7 +32,9 @@ func getFields(typ reflect.Type) []reflect.StructField { if newTyp.Kind() == reflect.Ptr { newTyp = newTyp.Elem() } - fields = append(fields, getFields(newTyp)...) + if newTyp.Kind() == reflect.Struct { + fields = append(fields, getFields(newTyp)...) + } } return fields diff --git a/schema/schema.go b/schema/schema.go index bc3c467a..16fef29b 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -200,7 +200,9 @@ func getFields(typ reflect.Type) []reflect.StructField { if newTyp.Kind() == reflect.Ptr { newTyp = newTyp.Elem() } - fields = append(fields, getFields(newTyp)...) + if newTyp.Kind() == reflect.Struct { + fields = append(fields, getFields(newTyp)...) + } } return fields @@ -542,6 +544,8 @@ func GenerateWithMode(t reflect.Type, mode Mode, schema *Schema) (*Schema, error return GenerateWithMode(t.Elem(), mode, schema) case reflect.Interface: // Interfaces can be any type. + case reflect.Uintptr, reflect.UnsafePointer, reflect.Func: + // Ignored... default: return nil, fmt.Errorf("unsupported type %s from %s", t.Kind(), t) }