Skip to content

Commit

Permalink
object/string: extend format to also support arrays and hashes as arg…
Browse files Browse the repository at this point in the history
…uments
  • Loading branch information
MarkusFreitag committed Nov 1, 2022
1 parent 82b2b13 commit 23e76c5
Showing 1 changed file with 4 additions and 14 deletions.
18 changes: 4 additions & 14 deletions object/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,28 +73,18 @@ func init() {
🚀 » "test%s".format("test")
» "testtest"`,
ArgPattern: Args(
OverloadArg(STRING_OBJ, INTEGER_OBJ, FLOAT_OBJ, BOOLEAN_OBJ), // first argument can be string or int
OverloadArg(STRING_OBJ, INTEGER_OBJ, FLOAT_OBJ, BOOLEAN_OBJ, ARRAY_OBJ, HASH_OBJ),
),
ReturnPattern: Args(
Arg(STRING_OBJ),
),
},
method: func(o Object, args []Object, _ Environment) Object {
s := o.(*String)
nativeObjects := []interface{}{}
for _, arg := range args {
switch e := arg.(type) {
case *String:
nativeObjects = append(nativeObjects, e.Value)
case *Integer:
nativeObjects = append(nativeObjects, e.Value)
case *Float:
nativeObjects = append(nativeObjects, e.Value)
case *Boolean:
nativeObjects = append(nativeObjects, e.Value)
}
nativeObjects := make([]any, len(args))
for idx, arg := range args {
nativeObjects[idx] = ObjectToAny(arg)
}

return NewString(fmt.Sprintf(s.Value, nativeObjects...))
},
},
Expand Down

0 comments on commit 23e76c5

Please sign in to comment.