/
transcribe-api.go
77 lines (64 loc) · 1.83 KB
/
transcribe-api.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
package js
import (
"fmt"
"io"
"strings"
"github.com/beevik/etree"
"github.com/tliron/go-ard"
"github.com/tliron/kutil/transcribe"
"github.com/tliron/yamlkeys"
)
type TranscribeAPI struct{}
func (self TranscribeAPI) ValidateFormat(code string, format string) error {
return transcribe.Validate(code, format)
}
func (self TranscribeAPI) Decode(code string, format string, all bool) (ard.Value, error) {
switch format {
case "yaml", "":
if all {
if value, err := yamlkeys.DecodeAll(strings.NewReader(code)); err == nil {
value_, _ := ard.NormalizeStringMaps(value)
return value_, err
} else {
return nil, err
}
} else {
value, _, err := ard.DecodeYAML(code, false)
value, _ = ard.NormalizeStringMaps(value)
return value, err
}
case "json":
value, _, err := ard.DecodeJSON(code, false)
value, _ = ard.NormalizeStringMaps(value)
return value, err
case "cjson":
value, _, err := ard.DecodeCompatibleJSON(code, false)
value, _ = ard.NormalizeStringMaps(value)
return value, err
case "xml":
value, _, err := ard.DecodeCompatibleXML(code, false)
value, _ = ard.NormalizeStringMaps(value)
return value, err
case "cbor":
value, _, err := ard.DecodeCBOR(code)
value, _ = ard.NormalizeStringMaps(value)
return value, err
case "messagepack":
value, _, err := ard.DecodeMessagePack(code)
value, _ = ard.NormalizeStringMaps(value)
return value, err
default:
return nil, fmt.Errorf("unsupported format: %q", format)
}
}
func (self TranscribeAPI) Encode(value any, format string, indent string, writer io.Writer) (string, error) {
if writer == nil {
return transcribe.Encode(value, format, indent, false)
} else {
err := transcribe.Write(value, format, indent, false, writer)
return "", err
}
}
func (self TranscribeAPI) NewXMLDocument() *etree.Document {
return etree.NewDocument()
}