Skip to content

Commit

Permalink
适配存在函数
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Dec 24, 2024
1 parent d9fb675 commit 7d20dc2
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 4 deletions.
5 changes: 5 additions & 0 deletions simplejson_must/simplejsonx_must.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
simpleJson = simplejsonx.Wrap(value)
return simpleJson
}

func List(items []interface{}) (elements []*simplejson.Json) {
elements = simplejsonx.List(items)
return elements
}
6 changes: 6 additions & 0 deletions simplejson_must/value_must.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ func Extract[T any](simpleJson *simplejson.Json, key string) T {
return res0
}

func Inspect[T any](simpleJson *simplejson.Json, key string) T {
res0, err := simplejsonx.Inspect[T](simpleJson, key)
sure.Must(err)
return res0
}

func Resolve[T any](simpleJson *simplejson.Json) T {
res0, err := simplejsonx.Resolve[T](simpleJson)
sure.Must(err)
Expand Down
5 changes: 5 additions & 0 deletions simplejson_omit/simplejsonx_omit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
simpleJson = simplejsonx.Wrap(value)
return simpleJson
}

func List(items []interface{}) (elements []*simplejson.Json) {
elements = simplejsonx.List(items)
return elements
}
6 changes: 6 additions & 0 deletions simplejson_omit/value_omit.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ func Extract[T any](simpleJson *simplejson.Json, key string) T {
return res0
}

func Inspect[T any](simpleJson *simplejson.Json, key string) T {
res0, err := simplejsonx.Inspect[T](simpleJson, key)
sure.Omit(err)
return res0
}

func Resolve[T any](simpleJson *simplejson.Json) T {
res0, err := simplejsonx.Resolve[T](simpleJson)
sure.Omit(err)
Expand Down
5 changes: 5 additions & 0 deletions simplejson_soft/simplejsonx_soft.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
simpleJson = simplejsonx.Wrap(value)
return simpleJson
}

func List(items []interface{}) (elements []*simplejson.Json) {
elements = simplejsonx.List(items)
return elements
}
6 changes: 6 additions & 0 deletions simplejson_soft/value_soft.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ func Extract[T any](simpleJson *simplejson.Json, key string) T {
return res0
}

func Inspect[T any](simpleJson *simplejson.Json, key string) T {
res0, err := simplejsonx.Inspect[T](simpleJson, key)
sure.Soft(err)
return res0
}

func Resolve[T any](simpleJson *simplejson.Json) T {
res0, err := simplejsonx.Resolve[T](simpleJson)
sure.Soft(err)
Expand Down
9 changes: 9 additions & 0 deletions simplejsonx.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,12 @@ func Wrap(value interface{}) (simpleJson *simplejson.Json) {
simpleJson.SetPath([]string{}, value)
return simpleJson
}

// List converts a slice of items into a slice of simplejson.Json objects.
func List(items []interface{}) (elements []*simplejson.Json) {
elements = make([]*simplejson.Json, 0, len(items))
for _, item := range items {
elements = append(elements, Wrap(item))
}
return elements
}
27 changes: 27 additions & 0 deletions simplejsonx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,30 @@ func TestWrap_InvalidNone(t *testing.T) {
require.Equal(t, "", res)
}
}

func TestList(t *testing.T) {
data := []byte(`{
"infos": [
{"code":1, "name":"a"},
{"code":2, "name":"b"},
{"code":3, "name":"c"}
],
"ranks": ["x", "y", "z"]
}`)
simpleJson, err := simplejsonx.Load(data)
require.NoError(t, err)
infos, err := simpleJson.Get("infos").Array()
require.NoError(t, err)
elements := simplejsonx.List(infos)

var resMap = map[string]int{}
for _, elem := range elements {
code, err := elem.Get("code").Int()
require.NoError(t, err)
name, err := elem.Get("name").String()
require.NoError(t, err)
t.Log(code, name)
resMap[name] = code
}
require.Equal(t, map[string]int{"a": 1, "b": 2, "c": 3}, resMap)
}
12 changes: 12 additions & 0 deletions value.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@ func Extract[T any](simpleJson *simplejson.Json, key string) (T, error) {
return Resolve[T](simpleJson.Get(key))
}

// Inspect retrieves the value of the given key if it exists in the JSON object, returning the zero value if the key is missing.
func Inspect[T any](simpleJson *simplejson.Json, key string) (T, error) {
if simpleJson == nil {
return tern.Zero[T](), errors.New("parameter simpleJson is missing")
}
value, exist := simpleJson.CheckGet(key)
if !exist {
return tern.Zero[T](), nil
}
return Resolve[T](value)
}

/*
Resolve extracts the value from the provided JSON and convert it to typed value.
Supports the following functions:
Expand Down
39 changes: 35 additions & 4 deletions value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,52 @@ import (
"github.com/yyle88/simplejsonx"
)

func TestResolve_Int(t *testing.T) {
func TestExtract_Int(t *testing.T) {
simpleJson, err := simplejsonx.Load([]byte(`{"age": 18}`))
require.NoError(t, err)

res, err := simplejsonx.Resolve[int](simpleJson.Get("age"))
res, err := simplejsonx.Extract[int](simpleJson, "age")
require.NoError(t, err)
t.Log(res)
require.Equal(t, 18, res)
}

func TestExtract_Int(t *testing.T) {
simpleJson, err := simplejsonx.Load([]byte(`{"age": 18}`))
func TestExtract_Mismatch(t *testing.T) {
simpleJson, err := simplejsonx.Load([]byte(`{"name": "yyle88"}`))
require.NoError(t, err)

res, err := simplejsonx.Extract[int](simpleJson, "age")
require.Error(t, err)
t.Log(err)
t.Log(res)
require.Equal(t, 0, res)
}

func TestInspect(t *testing.T) {
simpleJson, err := simplejsonx.Load([]byte(`{"age": 18}`))
require.NoError(t, err)

res, err := simplejsonx.Inspect[int](simpleJson, "age")
require.NoError(t, err)
t.Log(res)
require.Equal(t, 18, res)
}

func TestInspect_Mismatch(t *testing.T) {
simpleJson, err := simplejsonx.Load([]byte(`{"name": "yyle88"}`))
require.NoError(t, err)

res, err := simplejsonx.Inspect[int](simpleJson, "age")
require.NoError(t, err)
t.Log(res)
require.Equal(t, 0, res)
}

func TestResolve_Int(t *testing.T) {
simpleJson, err := simplejsonx.Load([]byte(`{"age": 18}`))
require.NoError(t, err)

res, err := simplejsonx.Resolve[int](simpleJson.Get("age"))
require.NoError(t, err)
t.Log(res)
require.Equal(t, 18, res)
Expand Down

0 comments on commit 7d20dc2

Please sign in to comment.