Skip to content

Commit

Permalink
feat: optimize all Array code.
Browse files Browse the repository at this point in the history
  • Loading branch information
Zhangguiguang committed Dec 6, 2023
1 parent e527056 commit 5a4be87
Show file tree
Hide file tree
Showing 21 changed files with 142 additions and 376 deletions.
135 changes: 11 additions & 124 deletions core/base/any.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package base
import (
"encoding/json"
"math/big"

"github.com/coming-chat/wallet-SDK/core/base/inter"
)

// exchange `Aniable Object` & `Any`
Expand Down Expand Up @@ -71,142 +73,27 @@ func (a *Any) GetUInt32() *BigInt { return &BigInt{new(big.Int).SetUint64(uint64
func (a *Any) GetUInt64() *BigInt { return &BigInt{new(big.Int).SetUint64(*a.Value.(*uint64))} }
func (a *Any) GetBigInt() *BigInt { return a.Value.(*BigInt) }

// MARK - AnyArray

type AnyArray struct {
Values []any
inter.AnyArray[*Any]
}

func NewAnyArray() *AnyArray {
return &AnyArray{Values: make([]any, 0)}
}

// `AnyArray` only support Marshal
func (a AnyArray) MarshalJSON() ([]byte, error) {
return json.Marshal(a.Values)
}

func (a *AnyArray) JsonString() (*OptionalString, error) {
return JsonString(a)
}

func (a *AnyArray) Count() int {
return len(a.Values)
return &AnyArray{[]*Any{}}
}

func (a *AnyArray) Append(any *Any) {
a.Values = append(a.Values, any.Value)
}

func (a *AnyArray) Remove(index int) {
a.Values = append(a.Values[:index], a.Values[index+1:]...)
}

func (a *AnyArray) SetValue(value *Any, index int) {
a.Values[index] = value.Value
}

func (a *AnyArray) Contains(any *Any) bool {
return a.IndexOf(any) != -1
}

// return -1 if not found
func (a *AnyArray) IndexOf(any *Any) int {
for idx, item := range a.Values {
if item == any.Value {
return idx
}
}
return -1
}

func (a *AnyArray) ValueOf(index int) *Any {
return &Any{Value: a.Values[index]}
}

func (a *AnyArray) String() string {
data, err := json.Marshal(a.Values)
if err != nil {
return "[]"
}
return string(data)
}

func (a *AnyArray) AsAny() *Any {
return &Any{a.Values}
}

func AsAnyArray(a *Any) *AnyArray {
if res, ok := a.Value.([]any); ok {
return &AnyArray{res}
}
return nil
}
// MARK - AnyMap

type AnyMap struct {
Values map[string]any
inter.AnyMap[string, *Any]
}

func NewAnyMap() *AnyMap {
return &AnyMap{Values: make(map[string]any)}
}

// `AnyMap` only support Marshal
func (a AnyMap) MarshalJSON() ([]byte, error) {
return json.Marshal(a.Values)
}

func (a *AnyMap) JsonString() (*OptionalString, error) {
return JsonString(a)
}

func (a *AnyMap) ValueOf(key string) *Any {
if v, ok := a.Values[key]; ok {
return &Any{v}
}
return nil
}

func (a *AnyMap) SetValue(value *Any, key string) {
a.Values[key] = value.Value
}

func (a *AnyMap) Remove(key string) *Any {
if v, ok := a.Values[key]; ok {
delete(a.Values, key)
return &Any{v}
}
return nil
}

func (a *AnyMap) HasKey(key string) bool {
_, ok := a.Values[key]
return ok
return &AnyMap{map[string]*Any{}}
}

func (a *AnyMap) Keys() *StringArray {
keys := make([]string, len(a.Values))
i := 0
for k := range a.Values {
keys[i] = k
i++
}
return &StringArray{Values: keys}
}

func (a *AnyMap) String() string {
data, err := json.Marshal(a.Values)
if err != nil {
return "{}"
}
return string(data)
}

func (a *AnyMap) AsAny() *Any {
return &Any{a.Values}
}

func AsAnyMap(a *Any) *AnyMap {
if res, ok := a.Value.(map[string]any); ok {
return &AnyMap{res}
}
return nil
keys := inter.KeysOf(a.AnyMap)
return &StringArray{keys}
}
28 changes: 14 additions & 14 deletions core/base/any_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,52 +44,52 @@ func TestAnyArray(t *testing.T) {

a2 := &Any{int(123)}
arr.Append(a2)
require.Equal(t, arr.ValueOf(0), a1)
require.Equal(t, arr.ValueOf(1), a2)
require.Equal(t, arr.ValueAt(0), a1)
require.Equal(t, arr.ValueAt(1), a2)

a3 := &Any{"abc"}
arr.SetValue(a3, 1)
require.Equal(t, arr.ValueOf(1), a3)
require.Equal(t, arr.String(), `[true,"abc"]`)
require.Equal(t, arr.ValueAt(1), a3)
require.Equal(t, arr.JsonString(), `[true,"abc"]`)
require.Equal(t, arr.Count(), 2)

a4 := &Any{uint16(456)}
arr.Append(a4)
require.Equal(t, arr.Count(), 3)
arr.Remove(0)
require.Equal(t, arr.Count(), 2)
require.Equal(t, arr.String(), `["abc",456]`)
require.Equal(t, arr.JsonString(), `["abc",456]`)

ap := &AnyPerson{Name: "GGG", Age: 22}
arr.Append(ap.AsAny())
t.Log(arr.String())
require.NotNil(t, AsAnyPerson(arr.ValueOf(2)))
require.Nil(t, AsAnyPerson(arr.ValueOf(0)))
t.Log(arr.JsonString())
require.NotNil(t, AsAnyPerson(arr.ValueAt(2)))
require.Nil(t, AsAnyPerson(arr.ValueAt(0)))
}

func TestAnyMap(t *testing.T) {
mp := NewAnyMap()

require.Equal(t, mp.String(), "{}")
require.Equal(t, mp.JsonString(), "{}")

a1 := &Any{true}
mp.SetValue(a1, "bbb")
require.Equal(t, mp.Keys().Count(), 1)
require.Equal(t, mp.Keys().String(), `["bbb"]`)
require.Equal(t, mp.Keys().JsonString(), `["bbb"]`)
require.Equal(t, mp.ValueOf("bbb"), a1)

a2 := &Any{"abcd"}
mp.SetValue(a2, "alpha")
t.Log(mp.String())
require.Equal(t, mp.HasKey("alpha"), true)
require.Equal(t, mp.HasKey("beta"), false)
t.Log(mp.JsonString())
require.Equal(t, mp.Contains("alpha"), true)
require.Equal(t, mp.Contains("beta"), false)

require.Nil(t, mp.Remove("notkey"))
require.Equal(t, mp.Remove("bbb"), a1)

ap := &AnyPerson{Name: "GGG"}
mp.SetValue(ap.AsAny(), "person")
t.Log(mp.String())
t.Log(mp.JsonString())
p := AsAnyPerson(mp.ValueOf("person"))
require.Equal(t, p.Name, "GGG")
}
Expand Down
18 changes: 15 additions & 3 deletions core/base/inter/util.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package inter

import "strings"
import (
"strings"
"unicode"
)

// IsValidHexString
func IsValidHexString(str string) (valid bool, length int) {
// IsHexString
func IsHexString(str string) (valid bool, length int) {
if strings.HasPrefix(str, "0x") || strings.HasPrefix(str, "0X") {
str = str[2:] // remove 0x prefix
}
Expand All @@ -15,3 +18,12 @@ func IsValidHexString(str string) (valid bool, length int) {
}
return true, len(str)
}

func IsASCII(str string) bool {
for _, c := range str {
if c > unicode.MaxASCII {
return false
}
}
return true
}
15 changes: 0 additions & 15 deletions core/base/jsonable.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ type Jsonable interface {
// You need to implement the following methods if your class name is Xxx
// func NewXxxWithJsonString(str string) (*Xxx, error)

// Maybe you can also implement this method
// func NewXxxArrayWithJsonString(str string) (*AnyArray, error)

// ====== template
// func (j *Xxx) JsonString() (*base.OptionalString, error) {
// return base.JsonString(j)
Expand All @@ -20,18 +17,6 @@ type Jsonable interface {
// err := base.FromJsonString(str, &o)
// return &o, err
// }
// func NewXxxArrayWithJsonString(str string) (*base.AnyArray, error) {
// var o []*Xxx
// err := base.FromJsonString(str, &o)
// if err != nil {
// return nil, err
// }
// arr := make([]any, len(o))
// for i, v := range o {
// arr[i] = v
// }
// return &base.AnyArray{Values: arr}, err
// }
}

func JsonString(o interface{}) (*OptionalString, error) {
Expand Down
30 changes: 14 additions & 16 deletions core/base/jsonable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ func NewJsonObjWithJsonString(str string) (*JsonObj, error) {
func NewJsonObjArrayWithJsonString(str string) (*AnyArray, error) {
var o []*JsonObj
err := FromJsonString(str, &o)
arr := make([]any, len(o))
arr := make([]*Any, len(o))
for i, v := range o {
arr[i] = v
arr[i] = &Any{Value: v}
}
return &AnyArray{Values: arr}, err
return &AnyArray{AnyArray: arr}, err
}

func (o *JsonObj) AsAny() *Any {
Expand Down Expand Up @@ -61,20 +61,19 @@ func TestJsonForAny(t *testing.T) {
// ======================
o2 := JsonObj{Name: "A22", Age: 17}

arr1 := AnyArray{Values: []any{a1, o2}} // a1 is Any, o2 is JsonObj
jsonStrArr1, err := arr1.JsonString()
require.Nil(t, err)
arr1 := AnyArray{[]*Any{a1, o2.AsAny()}} // a1 is Any, o2 is JsonObj
jsonStrArr1 := arr1.JsonString()
t.Log(jsonStrArr1)
arr2 := []JsonObj{o1, o2}
jsonStrArr2, err := JsonString(arr2)
require.Nil(t, err)

require.Equal(t, jsonStrArr1, jsonStrArr2)
t.Log(jsonStrArr1.Value)
require.Equal(t, jsonStrArr1, jsonStrArr2.Value)

// ======================= new array
objArray, err := NewJsonObjArrayWithJsonString(jsonStrArr1.Value)
objArray, err := NewJsonObjArrayWithJsonString(jsonStrArr1)
require.Nil(t, err)
t.Log(objArray.Values...)
t.Log(objArray.AnyArray)
}

func TestJsonForNestAny(t *testing.T) {
Expand All @@ -89,10 +88,9 @@ func TestJsonForNestAny(t *testing.T) {
t.Log(jsonStr.Value)

// ===============
arr1 := AnyArray{Values: []any{a1}}
arr2 := AnyArray{Values: []any{arr1}}
arr3 := AnyArray{Values: []any{arr2}}
jsonStrArr, err := arr3.JsonString()
require.Nil(t, err)
t.Log(jsonStrArr.Value)
arr1 := AnyArray{[]*Any{{a1}}}
arr2 := AnyArray{[]*Any{{arr1}}}
arr3 := AnyArray{[]*Any{{arr2}}}
jsonStrArr := arr3.JsonString()
t.Log(jsonStrArr)
}
30 changes: 0 additions & 30 deletions core/base/pageable.go

This file was deleted.

Loading

0 comments on commit 5a4be87

Please sign in to comment.