From f79693804ba02f31d7ddcb99e83091452e83d0c9 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Wed, 15 Mar 2023 15:08:56 +0800 Subject: [PATCH] refactor: make package variable defaultTagName unexported --- structutil/struct.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/structutil/struct.go b/structutil/struct.go index 69e1288..1b1d28d 100644 --- a/structutil/struct.go +++ b/structutil/struct.go @@ -7,7 +7,7 @@ import ( ) // DefaultTagName is the default tag for struct fields to lookup. -var DefaultTagName = "json" +var defaultTagName = "json" // Struct is abstract struct for provide several high level functions type Struct struct { @@ -18,15 +18,25 @@ type Struct struct { } // New returns a new *Struct -func New(value any) *Struct { +func New(value any, tagName ...string) *Struct { value = pointer.ExtractPointer(value) v := reflect.ValueOf(value) t := reflect.TypeOf(value) + + tn := defaultTagName + + if len(tagName) > 0 { + tn = tagName[0] + } + + // if need: can also set defaultTagName to tn across structutil package level + // defaultTagName = tn + return &Struct{ raw: value, rtype: t, rvalue: v, - TagName: DefaultTagName, + TagName: tn, } }