Skip to content

Commit

Permalink
Custom formatter based on go's fmt package. (#185)
Browse files Browse the repository at this point in the history
* Custom formatter based on go's fmt package.

* Cleanup

* Cleanup

* Added tengo.MaxStringLen check

* Cleanup
  • Loading branch information
geseq authored and d5 committed Apr 13, 2019
1 parent bb07fa1 commit b2df4f5
Show file tree
Hide file tree
Showing 5 changed files with 1,262 additions and 23 deletions.
27 changes: 27 additions & 0 deletions objects/builtin_format.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package objects

func builtinFormat(args ...Object) (Object, error) {
numArgs := len(args)
if numArgs == 0 {
return nil, ErrWrongNumArguments
}

format, ok := args[0].(*String)
if !ok {
return nil, ErrInvalidArgumentType{
Name: "format",
Expected: "string",
Found: args[0].TypeName(),
}
}
if numArgs == 1 {
return format, nil // okay to return 'format' directly as String is immutable
}

s, err := Format(format.Value, args[1:]...)
if err != nil {
return nil, err
}

return &String{Value: s}, nil
}
4 changes: 4 additions & 0 deletions objects/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,8 @@ var Builtins = []*BuiltinFunction{
Name: "type_name",
Value: builtinTypeName,
},
{
Name: "format",
Value: builtinFormat,
},
}
Loading

0 comments on commit b2df4f5

Please sign in to comment.