Pārlūkot izejas kodu

config: add OnlyTaggedField config, only process tagged fields in struct

李盼 8 gadi atpakaļ
vecāks
revīzija
c27f6f9350
2 mainītis faili ar 7 papildinājumiem un 1 dzēšanām
  1. 3 0
      feature_config.go
  2. 4 1
      feature_reflect_extension.go

+ 3 - 0
feature_config.go

@@ -18,6 +18,7 @@ type Config struct {
 	SortMapKeys                   bool
 	UseNumber                     bool
 	TagKey                        string
+	OnlyTaggedField               bool
 	ValidateJsonRawMessage        bool
 	ObjectFieldMustBeSimpleString bool
 }
@@ -27,6 +28,7 @@ type frozenConfig struct {
 	sortMapKeys                   bool
 	indentionStep                 int
 	objectFieldMustBeSimpleString bool
+	onlyTaggedField               bool
 	decoderCache                  unsafe.Pointer
 	encoderCache                  unsafe.Pointer
 	extensions                    []Extension
@@ -77,6 +79,7 @@ func (cfg Config) Froze() API {
 		sortMapKeys:                   cfg.SortMapKeys,
 		indentionStep:                 cfg.IndentionStep,
 		objectFieldMustBeSimpleString: cfg.ObjectFieldMustBeSimpleString,
+		onlyTaggedField:               cfg.OnlyTaggedField,
 		streamPool:                    make(chan *Stream, 16),
 		iteratorPool:                  make(chan *Iterator, 16),
 	}

+ 4 - 1
feature_reflect_extension.go

@@ -245,7 +245,10 @@ func describeStruct(cfg *frozenConfig, prefix string, typ reflect.Type) *StructD
 	bindings := []*Binding{}
 	for i := 0; i < typ.NumField(); i++ {
 		field := typ.Field(i)
-		tag := field.Tag.Get(cfg.getTagKey())
+		tag, hastag := field.Tag.Lookup(cfg.getTagKey())
+		if cfg.onlyTaggedField && !hastag {
+			continue
+		}
 		tagParts := strings.Split(tag, ",")
 		if tag == "-" {
 			continue