Skip to content

Commit

Permalink
✨ feat: Slice appends the Last and First methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sohaha committed Dec 26, 2023
1 parent ec92306 commit 4cab429
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
8 changes: 8 additions & 0 deletions zjson/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,14 @@ func (r *Res) Slice() ztype.SliceType {
return ztype.ToSlice(r.Value())
}

func (r *Res) SliceString() []string {
return r.Slice().String()
}

func (r *Res) SliceInt() []int {
return r.Slice().Int()
}

func (r *Res) Maps() ztype.Maps {
if !r.IsArray() {
return ztype.Maps{}
Expand Down
5 changes: 4 additions & 1 deletion zjson/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ func TestGet(t *testing.T) {
tt.EqualExit(true, Get(demo, "user").IsObject())
tt.EqualExit(true, Get(demo, "user").Exists())
tt.EqualExit("暴龙兽", Get(demo, "user").Map().Get("name").String())
tt.EqualExit("天使兽", Get(demo, "friends").Maps().Index(0).Get("name").String())
tt.EqualExit("天使兽", Get(demo, "friends").Maps().First().Get("name").String())
tt.EqualExit(Get(demo, "friends").Maps().First().Get("name").String(), Get(demo, "friends").Maps().Index(0).Get("name").String())
tt.EqualExit(true, other.IsArray())
tt.EqualExit(Get(demo, "friends.1").String(), Get(demo, "friends").Get("#(name=天女兽)").String())
tt.EqualExit(2, Get(demo, "friends.#").Int())
Expand All @@ -121,6 +122,8 @@ func TestGet(t *testing.T) {
tt.EqualExit("[\"天女兽\"]", Get(demo, "[friends.1.name]").String())
tt.EqualExit(false, Valid("{{}"))
tt.EqualExit(true, Valid(demo))
tt.EqualExit("阿古兽", Get(demo, "children").SliceString()[0])
tt.EqualExit(0, Get(demo, "children").SliceInt()[0])

ForEachLine(demo+demo, func(line *Res) bool {
return true
Expand Down
18 changes: 17 additions & 1 deletion ztype/slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,20 @@ func (s SliceType) MarshalJSON() ([]byte, error) {
}

func (s SliceType) Index(i int) Type {
if len(s) <= i {
if i < 0 || i >= len(s) {
return Type{}
}
return s[i]
}

func (s SliceType) Last() Type {
return s.Index(len(s) - 1)
}

func (s SliceType) First() Type {
return s.Index(0)
}

func (s SliceType) Value() []interface{} {
ss := make([]interface{}, 0, len(s))
for i := range s {
Expand Down Expand Up @@ -55,6 +63,14 @@ func (s SliceType) Maps() Maps {
return ss
}

// func (s SliceType) Slice() []SliceType {
// ss := make([]SliceType, 0, len(s))
// for i := range s {
// ss = append(ss, s[i].Slice())
// }
// return ss
// }

// Deprecated: please use ToSlice
func Slice(value interface{}) SliceType {
return ToSlice(value)
Expand Down
5 changes: 5 additions & 0 deletions ztype/slice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ func TestSlice(t *testing.T) {
value := "ddd"
res := Slice(value)
tt.Log(res)

res = Slice([]interface{}{"1", 2, 3.0})
tt.Equal("1", res.First().String())
tt.Equal("3", res.Last().String())

m := []map[string]interface{}{{"h": "ddd"}}
res = ToSlice(m)
tt.Log(res)
Expand Down

0 comments on commit 4cab429

Please sign in to comment.