/
table_lookup.go
67 lines (58 loc) · 1.92 KB
/
table_lookup.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"github.com/ysugimoto/falco/ast"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Table_lookup_Name = "table.lookup"
var Table_lookup_ArgumentTypes = []value.Type{value.IdentType, value.StringType, value.StringType}
func Table_lookup_Validate(args []value.Value) error {
if len(args) < 2 || len(args) > 3 {
return errors.ArgumentNotInRange(Table_lookup_Name, 2, 3, args)
}
for i := range args {
if args[i].Type() != Table_lookup_ArgumentTypes[i] {
return errors.TypeMismatch(Table_lookup_Name, i+1, Table_lookup_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
// Fastly built-in function implementation of table.lookup
// Arguments may be:
// - TABLE, STRING, STRING
// - TABLE, STRING
// Reference: https://developer.fastly.com/reference/vcl/functions/table/table-lookup/
func Table_lookup(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Table_lookup_Validate(args); err != nil {
return value.Null, err
}
id := value.Unwrap[*value.Ident](args[0]).Value
key := value.Unwrap[*value.String](args[1]).Value
defaultValue := &value.String{IsNotSet: true}
if len(args) > 2 {
// explicit clone value
v := value.Unwrap[*value.String](args[2]).Value
defaultValue = &value.String{Value: v}
}
table, ok := ctx.Tables[id]
if !ok {
return &value.String{IsNotSet: true}, errors.New(Table_lookup_Name,
"table %d does not exist", id,
)
}
for _, prop := range table.Properties {
if prop.Key.Value == key {
v, ok := prop.Value.(*ast.String)
if !ok {
return &value.String{IsNotSet: true}, errors.New(Table_lookup_Name,
"table %s value could not cast to STRING type", id,
)
}
return &value.String{Value: v.Value}, nil
}
}
return defaultValue, nil
}