...
1 package testsuite
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7
8 "github.com/pelletier/go-toml/v2"
9 )
10
11 type parser struct{}
12
13 func (p parser) Decode(input string) (output string, outputIsError bool, retErr error) {
14 defer func() {
15 if r := recover(); r != nil {
16 switch rr := r.(type) {
17 case error:
18 retErr = rr
19 default:
20 retErr = fmt.Errorf("%s", rr)
21 }
22 }
23 }()
24
25 var v interface{}
26
27 if err := toml.Unmarshal([]byte(input), &v); err != nil {
28 return err.Error(), true, nil
29 }
30
31 j, err := json.MarshalIndent(addTag("", v), "", " ")
32 if err != nil {
33 return "", false, retErr
34 }
35
36 return string(j), false, retErr
37 }
38
39 func (p parser) Encode(input string) (output string, outputIsError bool, retErr error) {
40 defer func() {
41 if r := recover(); r != nil {
42 switch rr := r.(type) {
43 case error:
44 retErr = rr
45 default:
46 retErr = fmt.Errorf("%s", rr)
47 }
48 }
49 }()
50
51 var tmp interface{}
52 err := json.Unmarshal([]byte(input), &tmp)
53 if err != nil {
54 return "", false, err
55 }
56
57 rm, err := rmTag(tmp)
58 if err != nil {
59 return err.Error(), true, retErr
60 }
61
62 buf := new(bytes.Buffer)
63 err = toml.NewEncoder(buf).Encode(rm)
64 if err != nil {
65 return err.Error(), true, retErr
66 }
67
68 return buf.String(), false, retErr
69 }
70
View as plain text