Added a StringListObject
Some checks failed
Test / build (push) Failing after 57s

This commit is contained in:
Leon Mika 2025-05-18 12:45:33 +10:00
parent 7d16701691
commit f4be44fcbc
3 changed files with 29 additions and 11 deletions

View file

@ -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) {
@ -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")
}
type StringSlice []string

View file

@ -140,17 +140,17 @@ func TestStrs_Split(t *testing.T) {
want any
wantErr bool
}{
{desc: "split 1", eval: `strs:split "1,2,3" ","`, want: builtins.StringSlice{"1", "2", "3"}},
{desc: "split 2", eval: `strs:split "1,2,3" ";"`, want: builtins.StringSlice{"1,2,3"}},
{desc: "split 3", eval: `strs:split "" ";"`, want: builtins.StringSlice{""}},
{desc: "split 4", eval: `strs:split " " ";"`, want: builtins.StringSlice{" "}},
{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: ucl.StringListObject{"1,2,3"}},
{desc: "split 3", eval: `strs:split "" ";"`, want: ucl.StringListObject{""}},
{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 2", eval: `strs:split "1,2,3" "," -max 5`, 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: 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},
}

View file

@ -80,6 +80,24 @@ func (s *ListObject) Index(i int) Object {
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 {
Iterable
}
@ -185,6 +203,8 @@ func toGoValue(obj Object) (interface{}, bool) {
return string(v), true
case IntObject:
return int(v), true
case StringListObject:
return []string(v), true
case boolObject:
return bool(v), true
case timeObject: