-
Notifications
You must be signed in to change notification settings - Fork 0
/
ard.go
74 lines (61 loc) · 1.46 KB
/
ard.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
package api
import (
"bytes"
"fmt"
"github.com/tliron/commonjs-goja"
"github.com/tliron/go-ard"
"github.com/tliron/yamlkeys"
)
// ([commonjs.CreateExtensionFunc] signature)
func CreateARDExtension(jsContext *commonjs.Context) any {
return ARD{}
}
//
// ARD
//
type ARD struct{}
func (self ARD) Decode(code []byte, format string, all bool) (ard.Value, error) {
switch format {
case "yaml":
if all {
if value, err := yamlkeys.DecodeAll(bytes.NewReader(code)); err == nil {
value_, _ := ard.ConvertMapsToStringMaps(value)
return value_, nil
} else {
return nil, err
}
} else {
if value, _, err := ard.DecodeYAML(code, false); err == nil {
value, _ = ard.ConvertMapsToStringMaps(value)
return value, nil
} else {
return nil, err
}
}
case "json":
return ard.DecodeJSON(code, true)
case "xjson":
return ard.DecodeXJSON(code, true)
case "xml":
if value, err := ard.DecodeXML(code); err == nil {
value, _ = ard.ConvertMapsToStringMaps(value)
return value, nil
} else {
return nil, err
}
case "cbor":
if value, err := ard.DecodeCBOR(code, false); err == nil {
value, _ = ard.ConvertMapsToStringMaps(value)
return value, nil
} else {
return nil, err
}
case "messagepack":
return ard.DecodeMessagePack(code, false, true)
default:
return nil, fmt.Errorf("unsupported format: %q", format)
}
}
func (self ARD) ValidateFormat(code []byte, format string) error {
return ard.Validate(code, format)
}