-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
69 lines (57 loc) · 1.4 KB
/
options.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
package ncserial
import "fmt"
//Option for nginx command
type Option []string
//MarshalD implememt Marshaler
func (o Option) MarshalD() ([]Directive, error) {
return []Directive{
NewOption(o),
}, nil
}
//Options represent list of words as kvoption
type Options []Option
//MarshalD implememt Marshaler
func (o Options) MarshalD() ([]Directive, error) {
var ds []Directive
for _, opt := range o {
ds = append(ds, NewOption(opt))
}
return ds, nil
}
//KeyValueOption A key/value directive. implements Directive
type KeyValueOption struct {
Base
name string
value string
}
//NewOption with given strings
func NewOption(words []string) *KeyValueOption {
if len(words) == 0 {
return nil // empty Option
} else if len(words) == 1 {
return NewKVOption(words[0], nil)
} else if len(words) == 2 {
return NewKVOption(words[0], words[1])
} else {
return NewKVOption(words[0], words[1:])
}
}
//NewKVOption init keyvalue option with key and give value
func NewKVOption(name string, value interface{}) *KeyValueOption {
return &KeyValueOption{
Base: NewDefaultBase(),
name: name,
value: CovertToString(value),
}
}
func (k KeyValueOption) String() string {
return k.Base.Render(fmt.Sprintf("%s %s;", k.name, k.value))
}
//Value implements Directive
func (k KeyValueOption) Value() interface{} {
return k.value
}
//Name implements Directive
func (k KeyValueOption) Name() string {
return k.name
}