This commit is contained in:
parent
7d16701691
commit
f4be44fcbc
|
@ -77,7 +77,7 @@ func split(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return StringSlice(strings.SplitN(s, sep, n)), nil
|
return ucl.StringListObject(strings.SplitN(s, sep, n)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func join(ctx context.Context, args ucl.CallArgs) (any, error) {
|
func join(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||||
|
@ -128,5 +128,3 @@ func join(ctx context.Context, args ucl.CallArgs) (any, error) {
|
||||||
|
|
||||||
return nil, errors.New("expected listable or iterable as arg 1")
|
return nil, errors.New("expected listable or iterable as arg 1")
|
||||||
}
|
}
|
||||||
|
|
||||||
type StringSlice []string
|
|
||||||
|
|
|
@ -140,17 +140,17 @@ func TestStrs_Split(t *testing.T) {
|
||||||
want any
|
want any
|
||||||
wantErr bool
|
wantErr bool
|
||||||
}{
|
}{
|
||||||
{desc: "split 1", eval: `strs:split "1,2,3" ","`, want: builtins.StringSlice{"1", "2", "3"}},
|
{desc: "split 1", eval: `strs:split "1,2,3" ","`, want: ucl.StringListObject{"1", "2", "3"}},
|
||||||
{desc: "split 2", eval: `strs:split "1,2,3" ";"`, want: builtins.StringSlice{"1,2,3"}},
|
{desc: "split 2", eval: `strs:split "1,2,3" ";"`, want: ucl.StringListObject{"1,2,3"}},
|
||||||
{desc: "split 3", eval: `strs:split "" ";"`, want: builtins.StringSlice{""}},
|
{desc: "split 3", eval: `strs:split "" ";"`, want: ucl.StringListObject{""}},
|
||||||
{desc: "split 4", eval: `strs:split " " ";"`, want: builtins.StringSlice{" "}},
|
{desc: "split 4", eval: `strs:split " " ";"`, want: ucl.StringListObject{" "}},
|
||||||
|
|
||||||
{desc: "split by char 1", eval: `strs:split "123"`, want: builtins.StringSlice{"1", "2", "3"}},
|
{desc: "split by char 1", eval: `strs:split "123"`, want: ucl.StringListObject{"1", "2", "3"}},
|
||||||
|
|
||||||
{desc: "split max 1", eval: `strs:split "1,2,3" "," -max 2`, want: builtins.StringSlice{"1", "2,3"}},
|
{desc: "split max 1", eval: `strs:split "1,2,3" "," -max 2`, want: ucl.StringListObject{"1", "2,3"}},
|
||||||
{desc: "split max 2", eval: `strs:split "1,2,3" "," -max 5`, want: builtins.StringSlice{"1", "2", "3"}},
|
{desc: "split max 2", eval: `strs:split "1,2,3" "," -max 5`, want: ucl.StringListObject{"1", "2", "3"}},
|
||||||
|
|
||||||
{desc: "split by char max 1", eval: `strs:split "12345" -max 3`, want: builtins.StringSlice{"1", "2", "345"}},
|
{desc: "split by char max 1", eval: `strs:split "12345" -max 3`, want: ucl.StringListObject{"1", "2", "345"}},
|
||||||
|
|
||||||
{desc: "err 1", eval: `strs:split "1,2,3" -max []`, wantErr: true},
|
{desc: "err 1", eval: `strs:split "1,2,3" -max []`, wantErr: true},
|
||||||
}
|
}
|
||||||
|
|
20
ucl/objs.go
20
ucl/objs.go
|
@ -80,6 +80,24 @@ func (s *ListObject) Index(i int) Object {
|
||||||
return (*s)[i]
|
return (*s)[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type StringListObject []string
|
||||||
|
|
||||||
|
func (ss StringListObject) String() string {
|
||||||
|
return fmt.Sprintf("[%v]", strings.Join(ss, " "))
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss StringListObject) Truthy() bool {
|
||||||
|
return len(ss) > 0
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss StringListObject) Len() int {
|
||||||
|
return len(ss)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ss StringListObject) Index(i int) Object {
|
||||||
|
return StringObject(ss[i])
|
||||||
|
}
|
||||||
|
|
||||||
type iteratorObject struct {
|
type iteratorObject struct {
|
||||||
Iterable
|
Iterable
|
||||||
}
|
}
|
||||||
|
@ -185,6 +203,8 @@ func toGoValue(obj Object) (interface{}, bool) {
|
||||||
return string(v), true
|
return string(v), true
|
||||||
case IntObject:
|
case IntObject:
|
||||||
return int(v), true
|
return int(v), true
|
||||||
|
case StringListObject:
|
||||||
|
return []string(v), true
|
||||||
case boolObject:
|
case boolObject:
|
||||||
return bool(v), true
|
return bool(v), true
|
||||||
case timeObject:
|
case timeObject:
|
||||||
|
|
Loading…
Reference in a new issue