From f295ec22fa837f3d03163b69ad3153f503bf629f Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Wed, 22 Jun 2022 23:55:42 +0800 Subject: [PATCH 1/2] feature(object): add int pointer --- object/object.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/object/object.go b/object/object.go index 7ce0a98..dc2b183 100644 --- a/object/object.go +++ b/object/object.go @@ -53,3 +53,8 @@ func Int64(i int64) *int64 { func Int32(i int32) *int32 { return &i } + +// Int 复制 int 对象,并返回复制体的指针 +func Int(i int) *int { + return &i +} From 68bcd65a6a831d34ad6b09846fa611722344e928 Mon Sep 17 00:00:00 2001 From: Matrix-X Date: Tue, 28 Jun 2022 22:30:45 +0800 Subject: [PATCH 2/2] refact(object): collection with hashmap pointer --- go.sum | 1 - object/collection.go | 40 ++++++++++++++++++++-------------------- object/unit.go | 11 +++++++++++ object/unti_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 73 insertions(+), 21 deletions(-) create mode 100644 object/unit.go create mode 100644 object/unti_test.go diff --git a/go.sum b/go.sum index 428b9ff..aaa2b3a 100644 --- a/go.sum +++ b/go.sum @@ -66,7 +66,6 @@ github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpO github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= -github.com/jinzhu/now v1.1.4 h1:tHnRBy1i5F2Dh8BAFxqFzxKqqvezXrL2OW1TnX+Mlas= github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ= github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8= diff --git a/object/collection.go b/object/collection.go index b7dce29..c985849 100644 --- a/object/collection.go +++ b/object/collection.go @@ -6,24 +6,24 @@ import ( ) type Collection struct { - items HashMap + items *HashMap } func NewCollection(items *HashMap) *Collection { return &Collection{ - items: *items, + items: items, } } func (c *Collection) All() *HashMap { - return &c.items + return c.items } func (c *Collection) Only(keys []string) (result *HashMap) { result = &HashMap{} - for key, value := range c.items { + for key, value := range *c.items { value = c.Get(key, nil) if value != nil { (*result)[key] = value @@ -63,13 +63,13 @@ func (c *Collection) Set(key string, value interface{}) { var segment string for len(segments) > 1 { segment, segments = segments[0], segments[1:] - if newItem[segment] == nil { - newItem[segment] = HashMap{} + if (*newItem)[segment] == nil { + (*newItem)[segment] = &HashMap{} } - newItem = newItem[segment].(HashMap) + newItem = (*newItem)[segment].(*HashMap) } - newItem[segments[0]] = value + (*newItem)[segments[0]] = value } func (c *Collection) GetBool(key string, defaultValue bool) bool { @@ -114,7 +114,7 @@ func (c *Collection) GetInt64(key string, defaultValue int64) int64 { func (c *Collection) GetString(key string, defaultValue string) string { strResult := c.Get(key, defaultValue).(string) - if strResult==""{ + if strResult == "" { strResult = defaultValue } return strResult @@ -165,8 +165,8 @@ func (c *Collection) Get(key string, defaultValue interface{}) interface{} { return &hashedObject } - if hashedObject[key] != nil { - return hashedObject[key] + if (*hashedObject)[key] != nil { + return (*hashedObject)[key] } else { result = defaultValue } @@ -174,18 +174,18 @@ func (c *Collection) Get(key string, defaultValue interface{}) interface{} { segments := strings.Split(key, ".") if len(segments) > 1 { for _, segment := range segments { - if hashedObject[segment] == nil { + if (*hashedObject)[segment] == nil { return defaultValue } else { - switch hashedObject[segment].(type) { - case HashMap: - hashedObject = hashedObject[segment].(HashMap) + switch (*hashedObject)[segment].(type) { case *HashMap: - hashedObject = *(hashedObject[segment].(*HashMap)) + hashedObject = (*hashedObject)[segment].(*HashMap) + case HashMap: + *hashedObject = (*hashedObject)[segment].(HashMap) case map[string]interface{}: - hashedObject = hashedObject[segment].(map[string]interface{}) + *hashedObject = (*hashedObject)[segment].(map[string]interface{}) default: - return hashedObject[segment] + return (*hashedObject)[segment] } } } @@ -211,10 +211,10 @@ func (c *Collection) ToString() string { } func (c *Collection) Count() int { - return len(c.items) + return len(*c.items) } -func (c *Collection) Unserialize(serialized string) HashMap { +func (c *Collection) Unserialize(serialized string) *HashMap { return c.items } diff --git a/object/unit.go b/object/unit.go new file mode 100644 index 0000000..58eb863 --- /dev/null +++ b/object/unit.go @@ -0,0 +1,11 @@ +package object + +const WX_CURRENCY_UNIT float64 = 100 + +func ConvertToCentUnit(amount float64) int { + return int(amount * WX_CURRENCY_UNIT) +} + +func ConvertToYuanUnit(amount int) float64 { + return float64(amount) / WX_CURRENCY_UNIT +} diff --git a/object/unti_test.go b/object/unti_test.go new file mode 100644 index 0000000..5fe6304 --- /dev/null +++ b/object/unti_test.go @@ -0,0 +1,42 @@ +package object + +import ( + "github.com/stretchr/testify/assert" + "testing" +) + +func Test_ConvertToCentUnit(t *testing.T) { + + var money int + money = ConvertToCentUnit(1.23) + assert.Equal(t, 123, money) + + money = ConvertToCentUnit(123) + assert.Equal(t, 12300, money) + + money = ConvertToCentUnit(1.23456) + assert.Equal(t, 123, money) + + money = ConvertToCentUnit(0.123456) + assert.Equal(t, 12, money) + + money = ConvertToCentUnit(0.163456) + assert.Equal(t, 16, money) + + money = ConvertToCentUnit(0.166456) + assert.Equal(t, 16, money) +} + +func Test_ConvertToYuanUnit(t *testing.T) { + + var money float64 + money = ConvertToYuanUnit(123) + assert.Equal(t, 1.23, money) + + money = ConvertToYuanUnit(12500) + assert.Equal(t, 125.0, money) + + money = ConvertToYuanUnit(163456) + assert.Equal(t, 1634.56, money) + +}