/
http_status_matches.go
66 lines (54 loc) · 1.77 KB
/
http_status_matches.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
// Code generated by __generator__/interpreter.go at once
package builtin
import (
"fmt"
"strings"
"github.com/ysugimoto/falco/interpreter/context"
"github.com/ysugimoto/falco/interpreter/function/errors"
"github.com/ysugimoto/falco/interpreter/value"
)
const Http_status_matches_Name = "http_status_matches"
var Http_status_matches_ArgumentTypes = []value.Type{value.IntegerType, value.StringType}
func Http_status_matches_Validate(args []value.Value) error {
if len(args) != 2 {
return errors.ArgumentNotEnough(Http_status_matches_Name, 2, args)
}
for i := range args {
if args[i].Type() != Http_status_matches_ArgumentTypes[i] {
return errors.TypeMismatch(Http_status_matches_Name, i+1, Http_status_matches_ArgumentTypes[i], args[i].Type())
}
}
return nil
}
// Fastly built-in function implementation of http_status_matches
// Arguments may be:
// - INTEGER, STRING
// Reference: https://developer.fastly.com/reference/vcl/functions/miscellaneous/http-status-matches/
func Http_status_matches(ctx *context.Context, args ...value.Value) (value.Value, error) {
// Argument validations
if err := Http_status_matches_Validate(args); err != nil {
return value.Null, err
}
status := fmt.Sprint(value.Unwrap[*value.Integer](args[0]).Value)
format := value.Unwrap[*value.String](args[1]).Value
var inverse bool
if format[0] == 0x21 { // prefixed with "!"
inverse = true
format = format[1:]
}
for _, code := range strings.Split(format, ",") {
if status != strings.TrimSpace(code) {
continue
}
// Matches, but consider inverse result
if inverse {
return &value.Boolean{Value: false}, nil
}
return &value.Boolean{Value: true}, nil
}
// No matches
if inverse {
return &value.Boolean{Value: true}, nil
}
return &value.Boolean{Value: false}, nil
}