-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
parser.go
134 lines (106 loc) · 3.26 KB
/
parser.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package core
import (
"encoding/json"
"errors"
yamlLex "github.com/goccy/go-yaml/lexer"
yamlParse "github.com/goccy/go-yaml/parser"
"github.com/inoxlang/inox/internal/mimeconsts"
"github.com/inoxlang/inox/internal/utils"
)
var (
parsers = map[Mimetype] /* no params */ StatelessParser{}
_ = []StatelessParser{(*jsonParser)(nil)}
)
func init() {
RegisterParser(mimeconsts.JSON_CTYPE, &jsonParser{})
RegisterParser(mimeconsts.APP_YAML_CTYPE, &yamlParser{})
}
// A StatelessParser represents a parser for a data format such as JSON or YAML.
// Implementations are allowed to use caching internally.
type StatelessParser interface {
Validate(ctx *Context, s string) bool
// Parse parses a string in the data format supported by the parser and returns the resulting value.
// Mutable returned values should never be stored to be returned later.
Parse(ctx *Context, s string) (Serializable, error)
}
func RegisterParser(mime Mimetype, p StatelessParser) {
if _, ok := parsers[mime]; ok {
panic(errors.New("a parser is already registered for mime " + string(mime)))
}
parsers[mime] = p
}
func GetParser(mime Mimetype) (StatelessParser, bool) {
p, ok := parsers[mime.WithoutParams()]
return p, ok
}
type jsonParser struct {
}
func (p *jsonParser) Validate(ctx *Context, s string) bool {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
panic(ErrTestedStringTooLarge)
}
return json.Valid(utils.StringAsBytes(s))
}
func (p *jsonParser) Parse(ctx *Context, s string) (Serializable, error) {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
return nil, ErrTestedStringTooLarge
}
var jsonVal any
err := json.Unmarshal(utils.StringAsBytes(s), &jsonVal)
if err != nil {
return nil, err
}
//TODO: use ParseJSONRepresentation (add tests before change)
return ConvertJSONValToInoxVal(jsonVal, false), nil
}
type yamlParser struct {
}
func (p *yamlParser) Validate(ctx *Context, s string) bool {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
panic(ErrTestedStringTooLarge)
}
tokens := yamlLex.Tokenize(s)
_, err := yamlParse.Parse(tokens, yamlParse.ParseComments)
return err == nil
}
func (p *yamlParser) Parse(ctx *Context, s string) (Serializable, error) {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
return nil, ErrTestedStringTooLarge
}
tokens := yamlLex.Tokenize(s)
yml, err := yamlParse.Parse(tokens, 0)
if err != nil {
return nil, err
}
return ConvertYamlParsedFileToInoxVal(ctx, yml, false), nil
}
type ulidParser struct {
}
func (p *ulidParser) Validate(ctx *Context, s string) bool {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
panic(ErrTestedStringTooLarge)
}
_, err := ParseULID(s)
return err == nil
}
func (p *ulidParser) Parse(ctx *Context, s string) (Serializable, error) {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
return nil, ErrTestedStringTooLarge
}
return ParseULID(s)
}
type uuidv4Parser struct {
}
func (p *uuidv4Parser) Validate(ctx *Context, s string) bool {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
panic(ErrTestedStringTooLarge)
}
_, err := ParseUUIDv4(s)
return err == nil
}
func (p *uuidv4Parser) Parse(ctx *Context, s string) (Serializable, error) {
if len(s) > DEFAULT_MAX_TESTED_STRING_BYTE_LENGTH {
return nil, ErrTestedStringTooLarge
}
return ParseUUIDv4(s)
}