summaryrefslogtreecommitdiffstats
path: root/vendor/github.com/d5/tengo/objects/builtin_format.go
blob: 1f0e75ed9761570a6465862028c12aeb651d182c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
}