From b1a8bce04189b20109ae7ecbd826fd4e5d4158fd Mon Sep 17 00:00:00 2001 From: Jay Date: Wed, 19 Dec 2018 11:26:59 +0800 Subject: [PATCH] add skip private field --- utils.go | 5 +++++ utils_test.go | 7 +++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/utils.go b/utils.go index 479934e..a5d3e6d 100644 --- a/utils.go +++ b/utils.go @@ -62,11 +62,16 @@ func ToMap(ss interface{}) map[string]interface{} { smap := make(map[string]interface{}) mtag := regexp.MustCompile(`cc:\"(.+)\"`) + re := regexp.MustCompile(`^[A-Z]`) + for i := 0; i < t.NumField(); i++ { f := t.Field(i) tag := string(t.Type().Field(i).Tag) str := mtag.FindStringSubmatch(tag) name := t.Type().Field(i).Name + if !re.Match([]byte(name)) { + continue + } leveling := false if len(str) > 1 { strArr := strings.Split(str[1], ",") diff --git a/utils_test.go b/utils_test.go index f9f1224..179bf2f 100644 --- a/utils_test.go +++ b/utils_test.go @@ -11,13 +11,14 @@ type Commands struct { Message string `db:"message" cc:"message"` Group string `db:"group" cc:"group"` Ctime time.Time `db:"ctime" cc:"ctime"` - Mtime time.Time `db:"mtime" cc:"ctime"` + Mtime time.Time `db:"mtime" cc:"mtime"` } // CommandsWithGroup - type CommandsWithGroup struct { Commands `cc:"-,<<"` GroupName string `db:"group_name" cc:"group_name"` + private string } func TestToMap(t *testing.T) { @@ -31,8 +32,10 @@ func TestToMap(t *testing.T) { cmdWGroup := CommandsWithGroup{} cmdWGroup.Commands = cmd cmdWGroup.GroupName = "asd" + cmdWGroup.private = "123" - ToMap(cmdWGroup) + m := ToMap(cmdWGroup) + t.Log(m) } func TestCalcPage(t *testing.T) {