Skip to content

Commit

Permalink
Merge pull request #17 from ArtisanCloud/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Matrix-X authored Jun 28, 2022
2 parents ebc9a5b + 68bcd65 commit 6d8e35e
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 21 deletions.
1 change: 0 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
40 changes: 20 additions & 20 deletions object/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -165,27 +165,27 @@ 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
}

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]
}
}
}
Expand All @@ -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
}
5 changes: 5 additions & 0 deletions object/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
11 changes: 11 additions & 0 deletions object/unit.go
Original file line number Diff line number Diff line change
@@ -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
}
42 changes: 42 additions & 0 deletions object/unti_test.go
Original file line number Diff line number Diff line change
@@ -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)

}

0 comments on commit 6d8e35e

Please sign in to comment.