Skip to content

BUGFIX: allow json tag overrides with set naming strategy #275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions extra/naming_strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ type namingStrategyExtension struct {

func (extension *namingStrategyExtension) UpdateStructDescriptor(structDescriptor *jsoniter.StructDescriptor) {
for _, binding := range structDescriptor.Fields {
binding.ToNames = []string{extension.translate(binding.Field.Name())}
binding.FromNames = []string{extension.translate(binding.Field.Name())}
if !binding.IgnoreOverride {
binding.ToNames = []string{extension.translate(binding.Field.Name())}
binding.FromNames = []string{extension.translate(binding.Field.Name())}
}
}
}

Expand Down
36 changes: 21 additions & 15 deletions reflect_extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@ func (structDescriptor *StructDescriptor) GetField(fieldName string) *Binding {

// Binding describe how should we encode/decode the struct field
type Binding struct {
levels []int
Field reflect2.StructField
FromNames []string
ToNames []string
Encoder ValEncoder
Decoder ValDecoder
levels []int
Field reflect2.StructField
FromNames []string
ToNames []string
Encoder ValEncoder
Decoder ValDecoder
IgnoreOverride bool
}

// Extension the one for all SPI. Customize encoding/decoding by specifying alternate encoder/decoder.
Expand Down Expand Up @@ -363,7 +364,7 @@ func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
}
}
}
fieldNames := calcFieldNames(field.Name(), tagParts[0], tag)
fieldNames, ignoreNameOverrides := calcFieldNames(field.Name(), tagParts[0], tag)
fieldCacheKey := fmt.Sprintf("%s/%s", typ.String(), field.Name())
decoder := fieldDecoders[fieldCacheKey]
if decoder == nil {
Expand All @@ -374,11 +375,12 @@ func describeStruct(ctx *ctx, typ reflect2.Type) *StructDescriptor {
encoder = encoderOfType(ctx.append(field.Name()), field.Type())
}
binding := &Binding{
Field: field,
FromNames: fieldNames,
ToNames: fieldNames,
Decoder: decoder,
Encoder: encoder,
Field: field,
FromNames: fieldNames,
ToNames: fieldNames,
Decoder: decoder,
Encoder: encoder,
IgnoreOverride: ignoreNameOverrides,
}
binding.levels = []int{i}
bindings = append(bindings, binding)
Expand Down Expand Up @@ -450,22 +452,26 @@ func processTags(structDescriptor *StructDescriptor, cfg *frozenConfig) {
}
}

func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string {
func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) ([]string, bool) {
var ignoreNamingStrategy bool // conditions for which we should ignore custom naming strategy
// ignore?
if wholeTag == "-" {
return []string{}
ignoreNamingStrategy = true
return []string{}, ignoreNamingStrategy
}
// rename?
var fieldNames []string
if tagProvidedFieldName == "" {
fieldNames = []string{originalFieldName}
} else {
ignoreNamingStrategy = true
fieldNames = []string{tagProvidedFieldName}

}
// private?
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
if isNotExported {
fieldNames = []string{}
}
return fieldNames
return fieldNames, ignoreNamingStrategy
}