...
1
2
3 package testsuite
4
5 import (
6 "encoding/json"
7 "fmt"
8 "os"
9
10 "github.com/pelletier/go-toml/v2"
11 )
12
13
14
15
16 func Marshal(v interface{}) ([]byte, error) {
17 return toml.Marshal(v)
18 }
19
20
21
22
23 func Unmarshal(data []byte, v interface{}) error {
24 return toml.Unmarshal(data, v)
25 }
26
27
28
29 func ValueToTaggedJSON(doc interface{}) ([]byte, error) {
30 return json.MarshalIndent(addTag("", doc), "", " ")
31 }
32
33
34
35
36 func DecodeStdin() error {
37 var decoded map[string]interface{}
38
39 if err := toml.NewDecoder(os.Stdin).Decode(&decoded); err != nil {
40 return fmt.Errorf("Error decoding TOML: %s", err)
41 }
42
43 j := json.NewEncoder(os.Stdout)
44 j.SetIndent("", " ")
45 if err := j.Encode(addTag("", decoded)); err != nil {
46 return fmt.Errorf("Error encoding JSON: %s", err)
47 }
48
49 return nil
50 }
51
52
53
54
55 func EncodeStdin() error {
56 var j interface{}
57 err := json.NewDecoder(os.Stdin).Decode(&j)
58 if err != nil {
59 return err
60 }
61
62 rm, err := rmTag(j)
63 if err != nil {
64 return fmt.Errorf("removing tags: %w", err)
65 }
66
67 return toml.NewEncoder(os.Stdout).Encode(rm)
68 }
69
View as plain text