mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
fix #260, support rename for extra.SupportPrivateFields
This commit is contained in:
parent
51dd70305b
commit
f246f80f14
@ -3,6 +3,7 @@ package extra
|
|||||||
import (
|
import (
|
||||||
"github.com/json-iterator/go"
|
"github.com/json-iterator/go"
|
||||||
"unicode"
|
"unicode"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
// SupportPrivateFields include private fields when encoding/decoding
|
// SupportPrivateFields include private fields when encoding/decoding
|
||||||
@ -18,8 +19,37 @@ func (extension *privateFieldsExtension) UpdateStructDescriptor(structDescriptor
|
|||||||
for _, binding := range structDescriptor.Fields {
|
for _, binding := range structDescriptor.Fields {
|
||||||
isPrivate := unicode.IsLower(rune(binding.Field.Name()[0]))
|
isPrivate := unicode.IsLower(rune(binding.Field.Name()[0]))
|
||||||
if isPrivate {
|
if isPrivate {
|
||||||
|
tag, hastag := binding.Field.Tag().Lookup("json")
|
||||||
|
if !hastag {
|
||||||
binding.FromNames = []string{binding.Field.Name()}
|
binding.FromNames = []string{binding.Field.Name()}
|
||||||
binding.ToNames = []string{binding.Field.Name()}
|
binding.ToNames = []string{binding.Field.Name()}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
tagParts := strings.Split(tag, ",")
|
||||||
|
names := calcFieldNames(binding.Field.Name(), tagParts[0], tag)
|
||||||
|
binding.FromNames = names
|
||||||
|
binding.ToNames = names
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func calcFieldNames(originalFieldName string, tagProvidedFieldName string, wholeTag string) []string {
|
||||||
|
// ignore?
|
||||||
|
if wholeTag == "-" {
|
||||||
|
return []string{}
|
||||||
|
}
|
||||||
|
// rename?
|
||||||
|
var fieldNames []string
|
||||||
|
if tagProvidedFieldName == "" {
|
||||||
|
fieldNames = []string{originalFieldName}
|
||||||
|
} else {
|
||||||
|
fieldNames = []string{tagProvidedFieldName}
|
||||||
|
}
|
||||||
|
// private?
|
||||||
|
isNotExported := unicode.IsLower(rune(originalFieldName[0]))
|
||||||
|
if isNotExported {
|
||||||
|
fieldNames = []string{}
|
||||||
|
}
|
||||||
|
return fieldNames
|
||||||
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user