-
Notifications
You must be signed in to change notification settings - Fork 0
/
arguments.go
70 lines (56 loc) · 1.32 KB
/
arguments.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
package launcher
import (
"encoding/json"
"fmt"
"github.com/mitchellh/mapstructure"
)
type Argument struct {
Rules Rules `mapstructure:"rules"`
Value []string `mapstructure:"value"`
}
func (a *Argument) UnmarshalJSON(data []byte) error {
var val interface{}
if err := json.Unmarshal(data, &val); err != nil {
return err
}
switch t := val.(type) {
case string:
*a = Argument{
Rules: []Rule{},
Value: []string{val.(string)},
}
case map[string]interface{}:
// I hate it here
if flatValue, isFlatValue := t["value"].(string); isFlatValue {
t["value"] = []string{flatValue}
}
if err := mapstructure.Decode(t, &a); err != nil {
return err
}
default:
return fmt.Errorf("illegal value - %v", t)
}
return nil
}
func (a Argument) MarshalJSON() ([]byte, error) {
if len(a.Rules) == 0 && len(a.Value) == 1 {
return json.Marshal(a.Value[0])
} else {
var mapped map[string]interface{}
if err := mapstructure.Decode(a, &mapped); err != nil {
return nil, err
}
// special flattening case for moM
// why do they even need that, it literally saves 2 BYTES!!!
if value, ok := mapped["value"].([]string); ok {
if len(value) == 1 {
mapped["value"] = value[0]
}
}
return json.Marshal(mapped)
}
}
type Arguments struct {
Game []Argument `json:"game"`
JVM []Argument `json:"jvm"`
}