Skip to content

Commit

Permalink
support ignore json tag
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Oct 27, 2024
1 parent c7a44d4 commit 9a515ee
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
29 changes: 19 additions & 10 deletions cmd/hasura-ndc-go/command/internal/schema_type_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,10 @@ func (tp *TypeParser) parseStructType(objectInfo *ObjectInfo, inferredType *type
for i := 0; i < inferredType.NumFields(); i++ {
fieldVar := inferredType.Field(i)
fieldTag := inferredType.Tag(i)

fieldKey, jsonOption := getFieldNameOrTag(fieldVar.Name(), fieldTag)
if jsonOption == jsonIgnore {
continue
}
fieldParser := NewTypeParser(tp.schemaParser, &Field{
Name: fieldVar.Name(),
Embedded: fieldVar.Embedded(),
Expand All @@ -332,7 +335,6 @@ func (tp *TypeParser) parseStructType(objectInfo *ObjectInfo, inferredType *type
if err != nil {
return err
}
fieldKey, nullable := getFieldNameOrTag(fieldVar.Name(), fieldTag)
embeddedObject, ok := tp.schemaParser.rawSchema.Objects[field.Type.SchemaName(false)]
if field.Embedded && ok {
// flatten embedded object fields to the parent object
Expand All @@ -341,7 +343,7 @@ func (tp *TypeParser) parseStructType(objectInfo *ObjectInfo, inferredType *type
}
} else {
fieldSchema := field.Type.Schema()
if nullable && field.Type.Kind() != schema.TypeNullable {
if jsonOption == jsonOmitEmpty && field.Type.Kind() != schema.TypeNullable {
fieldSchema = schema.NewNullableType(fieldSchema)
}
objectInfo.SchemaFields[fieldKey] = schema.ObjectField{
Expand Down Expand Up @@ -519,27 +521,34 @@ func parseTypeFromString(input string) (Type, error) {
return NewNamedType(typeInfo.Name, typeInfo), nil
}

const (
jsonOmitEmpty = "omitempty"
jsonIgnore = "-"
)

// get field name by json tag
// return the struct field name if not exist.
func getFieldNameOrTag(name string, tag string) (string, bool) {
func getFieldNameOrTag(name string, tag string) (string, string) {
if tag == "" {
return name, false
return name, ""
}
tags, err := structtag.Parse(tag)
if err != nil {
log.Warn().Err(err).Msgf("failed to parse tag of struct field: %s", name)
return name, false
return name, ""
}

jsonTag, err := tags.Get("json")
if err != nil {
log.Warn().Err(err).Msgf("json tag does not exist in struct field: %s", name)
return name, false
return name, ""
}
if jsonTag.Value() == "-" {
return name, jsonIgnore
}

nameParts := strings.Split(jsonTag.Value(), ",")
if len(nameParts) == 1 {
return jsonTag.Name, false
return jsonTag.Name, ""
}
return nameParts[0], nameParts[1] == "omitempty"
return nameParts[0], nameParts[1]
}
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ type GetTypesArguments struct {
ArrayBigIntPtrEmpty []*scalar.BigInt `json:"array_bigint_ptr_empty,omitempty"`
ArrayTimeEmpty []time.Time `json:"array_time_empty,omitempty"`
ArrayTimePtrEmpty []*time.Time `json:"array_time_ptr_empty,omitempty"`

IgnoredField string `json:"-"`
}

func FunctionGetTypes(ctx context.Context, state *types.State, arguments *GetTypesArguments) (*GetTypesArguments, error) {
Expand Down

0 comments on commit 9a515ee

Please sign in to comment.