-
-
Notifications
You must be signed in to change notification settings - Fork 23
/
std_atof.go
47 lines (39 loc) · 1.27 KB
/
std_atof.go
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"strconv"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Std_atof_Name = "std.atof"
var Std_atof_ArgumentTypes = []value.Type{value.StringType}
func Std_atof_Validate(args []value.Value) error {
if len(args) != 1 {
return errors.ArgumentNotEnough(Std_atof_Name, 1, args)
}
for i := range args {
if args[i].Type() != Std_atof_ArgumentTypes[i] {
return errors.TypeMismatch(Std_atof_Name, i+1, Std_atof_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
// Fastly built-in function implementation of std.atof
// Arguments may be:
// - STRING
// Reference: https://developer.fastly.com/reference/vcl/functions/strings/std-atof/
func Std_atof(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Std_atof_Validate(args); err != nil {
return value.Null, err
}
s := value.Unwrap[*value.String](args[0])
f, err := strconv.ParseFloat(s.Value, 64)
if err != nil {
return &value.Float{Value: 0}, errors.New(
Std_atof_Name, "Failed to parse float value: %s, %s", s.Value, err.Error(),
)
}
return &value.Float{Value: f}, nil
}