From 96816c6cad2bd3064807e962aca28d46209b2300 Mon Sep 17 00:00:00 2001 From: Bagus Cahyono Date: Thu, 17 Sep 2020 15:57:28 +0700 Subject: [PATCH 1/3] Use `DefaultCase` in `ToMTag` function --- m.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/m.go b/m.go index d44a619..0263c79 100644 --- a/m.go +++ b/m.go @@ -86,7 +86,7 @@ func ToM(data interface{}) (M, error) { } func ToMTag(data interface{}, tagName string) (M, error) { - return tomTagName(data, CaseLower, tagName) + return tomTagName(data, DefaultCase, tagName) } var _tagName string From 0066e3f3770103154e6e133e07440c07ab2ba85b Mon Sep 17 00:00:00 2001 From: Bagus Cahyono Date: Thu, 17 Sep 2020 16:18:22 +0700 Subject: [PATCH 2/3] User first tag name incase of something like `bson:"_id,omitempty"` --- m.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/m.go b/m.go index 0263c79..ce672c6 100644 --- a/m.go +++ b/m.go @@ -119,7 +119,7 @@ func tomTagName(data interface{}, namePattern string, tagName string) (M, error) f := rv.Type().Field(i) fieldName := f.Name if f.Tag.Get(tagName) != "" { - fieldName = f.Tag.Get(tagName) + fieldName = strings.Split(f.Tag.Get(tagName), ",")[0] } if fieldName == "-" { continue From 85750f338fcc78d0058e99e5bdd688b428f74e71 Mon Sep 17 00:00:00 2001 From: Noval Agung Prayogo Date: Mon, 25 Jan 2021 20:00:43 +0700 Subject: [PATCH 3/3] feat check whether field is castable --- m.go | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/m.go b/m.go index ce672c6..2e7d25a 100644 --- a/m.go +++ b/m.go @@ -135,13 +135,16 @@ func tomTagName(data interface{}, namePattern string, tagName string) (M, error) // If the type is struct but not time.Time or is a map if (f.Type.Kind() == reflect.Struct && f.Type != reflect.TypeOf(time.Time{})) || f.Type.Kind() == reflect.Map { + // Then we need to call this function again to fetch the sub value - subRes, err := tom(rv.Field(i).Interface(), namePattern) - if err != nil { - return nil, err - } + if rv.Field(i).CanInterface() { + subRes, err := tom(rv.Field(i).Interface(), namePattern) + if err != nil { + return nil, err + } - res[fieldName] = subRes + res[fieldName] = subRes + } // Skip the rest continue