Skip to content

Commit

Permalink
Add FieldNameTag config
Browse files Browse the repository at this point in the history
Allows field names to be based on a different tag.
Json is still the default tag so this is not a breaking change.
#28 (reply in thread)
  • Loading branch information
mathewTH committed Jan 5, 2023
1 parent 9f28aff commit da6de4c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,9 @@ type Reflector struct {
// root as opposed to a definition with a reference.
ExpandedStruct bool

// FieldNameTag will change the tag used to get field names. json tags are used by default.
FieldNameTag string

// IgnoredTypes defines a slice of types that should be ignored in the schema,
// switching to just allowing additional properties instead.
IgnoredTypes []interface{}
Expand Down Expand Up @@ -989,7 +992,11 @@ func ignoredByJSONSchemaTags(tags []string) bool {
}

func (r *Reflector) reflectFieldName(f reflect.StructField) (string, bool, bool, bool) {
jsonTagString, _ := f.Tag.Lookup("json")
tagKey := r.FieldNameTag
if tagKey == "" {
tagKey = "json"
}
jsonTagString := f.Tag.Get(tagKey)
jsonTags := strings.Split(jsonTagString, ",")

if ignoredByJSONTags(jsonTags) {
Expand Down

0 comments on commit da6de4c

Please sign in to comment.