-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
53 lines (47 loc) · 1.29 KB
/
common.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
package common
import (
"encoding/json"
"errors"
"github.com/hopeio/cherry/utils/encoding"
"github.com/pelletier/go-toml/v2"
"gopkg.in/yaml.v3"
"strings"
)
func Unmarshal(format encoding.Format, data []byte, v any) (encoding.Format, error) {
switch {
case format == encoding.Yaml || format == encoding.Yml:
return format, yaml.Unmarshal(data, v)
case format == encoding.Toml:
return format, toml.Unmarshal(data, v)
case format == encoding.Json:
return format, json.Unmarshal(data, v)
default:
if err := toml.Unmarshal(data, v); err == nil {
return encoding.Toml, nil
}
if err := json.Unmarshal(data, v); err == nil {
return encoding.Json, nil
} else if strings.Contains(err.Error(), "json: unknown field") {
return encoding.Json, err
}
yamlError := yaml.Unmarshal(data, v)
if yamlError == nil {
return encoding.Yaml, nil
} else if yErr, ok := yamlError.(*yaml.TypeError); ok {
return encoding.Yaml, yErr
}
return "", errors.New("failed to decode")
}
}
func Marshal(format encoding.Format, v any) ([]byte, error) {
switch {
case format == encoding.Yaml || format == encoding.Yml:
return yaml.Marshal(v)
case format == encoding.Toml:
return toml.Marshal(v)
case format == encoding.Json:
return json.Marshal(v)
default:
return toml.Marshal(v)
}
}