Skip to content

Commit d9a5048

Browse files
Merge pull request #6 from andreimerlescu/hotfix/new-flesh
Fixed issue with lists by adding a policy
2 parents b2da97c + cd11f2a commit d9a5048

File tree

6 files changed

+92
-26
lines changed

6 files changed

+92
-26
lines changed

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v2.0.6
1+
v2.0.7

figs.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ func With(opts Options) Plant {
7474
problems: make([]error, 0),
7575
figs: make(map[string]*figFruit),
7676
withered: make(map[string]witheredFig),
77+
sources: make(map[string]SourceConfig),
78+
sourceLocker: sync.RWMutex{},
7779
mu: sync.RWMutex{},
7880
mutationsCh: make(chan Mutation),
7981
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),

internals_test.go

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,16 @@ func TestTree_checkAndSetFromEnv(t *testing.T) {
1717
// create a new fig tree
1818
var figs *figTree
1919
figs = &figTree{
20-
harvest: 1,
21-
figs: make(map[string]*figFruit),
22-
tracking: false,
23-
withered: make(map[string]witheredFig),
24-
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
25-
mu: sync.RWMutex{},
26-
mutationsCh: make(chan Mutation, 1),
27-
filterTests: true,
20+
harvest: 1,
21+
figs: make(map[string]*figFruit),
22+
tracking: false,
23+
withered: make(map[string]witheredFig),
24+
sources: make(map[string]SourceConfig),
25+
sourceLocker: sync.RWMutex{},
26+
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
27+
mu: sync.RWMutex{},
28+
mutationsCh: make(chan Mutation, 1),
29+
filterTests: true,
2830
}
2931

3032
// assign an int to k
@@ -61,6 +63,8 @@ func TestTree_setValue(t *testing.T) {
6163
ConfigFilePath string
6264
figs map[string]*figFruit
6365
withered map[string]witheredFig
66+
sources map[string]SourceConfig
67+
sourceLocker sync.RWMutex
6468
mu sync.RWMutex
6569
tracking bool
6670
mutationsCh chan Mutation
@@ -79,9 +83,11 @@ func TestTree_setValue(t *testing.T) {
7983
{
8084
name: "Set int value",
8185
fields: fields{
82-
figs: make(map[string]*figFruit),
83-
withered: make(map[string]witheredFig),
84-
mutationsCh: make(chan Mutation, 1),
86+
figs: make(map[string]*figFruit),
87+
withered: make(map[string]witheredFig),
88+
mutationsCh: make(chan Mutation, 1),
89+
sources: make(map[string]SourceConfig),
90+
sourceLocker: sync.RWMutex{},
8591
},
8692
args: args{
8793
flagVal: new(int),
@@ -93,9 +99,11 @@ func TestTree_setValue(t *testing.T) {
9399
{
94100
name: "Set string value",
95101
fields: fields{
96-
figs: make(map[string]*figFruit),
97-
withered: make(map[string]witheredFig),
98-
mutationsCh: make(chan Mutation, 1),
102+
figs: make(map[string]*figFruit),
103+
withered: make(map[string]witheredFig),
104+
mutationsCh: make(chan Mutation, 1),
105+
sources: make(map[string]SourceConfig),
106+
sourceLocker: sync.RWMutex{},
99107
},
100108
args: args{
101109
flagVal: new(string),
@@ -107,9 +115,11 @@ func TestTree_setValue(t *testing.T) {
107115
{
108116
name: "Invalid type",
109117
fields: fields{
110-
figs: make(map[string]*figFruit),
111-
withered: make(map[string]witheredFig),
112-
mutationsCh: make(chan Mutation, 1),
118+
figs: make(map[string]*figFruit),
119+
withered: make(map[string]witheredFig),
120+
mutationsCh: make(chan Mutation, 1),
121+
sources: make(map[string]SourceConfig),
122+
sourceLocker: sync.RWMutex{},
113123
},
114124
args: args{
115125
flagVal: new(float32), // Unsupported type
@@ -150,13 +160,15 @@ func TestTree_setValue(t *testing.T) {
150160

151161
func TestTree_setValuesFromMap(t *testing.T) {
152162
tree := &figTree{
153-
figs: make(map[string]*figFruit),
154-
withered: make(map[string]witheredFig),
155-
mu: sync.RWMutex{},
156-
tracking: false,
157-
mutationsCh: make(chan Mutation, 1),
158-
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
159-
filterTests: true,
163+
figs: make(map[string]*figFruit),
164+
withered: make(map[string]witheredFig),
165+
sources: make(map[string]SourceConfig),
166+
sourceLocker: sync.RWMutex{},
167+
mu: sync.RWMutex{},
168+
tracking: false,
169+
mutationsCh: make(chan Mutation, 1),
170+
flagSet: flag.NewFlagSet(os.Args[0], flag.ContinueOnError),
171+
filterTests: true,
160172
}
161173
m := map[string]interface{}{
162174
"name": "yahuah",

list_flag.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ func (l *ListFlag) String() string {
3434
return strings.Join(*l.values, ",")
3535
}
3636

37+
// PolicyListAppend will apply ListFlag.Set to the list of values and not append to any existing values in the ListFlag
38+
var PolicyListAppend bool = false
39+
3740
// Set unpacks a comma separated value argument and appends items to the list of []string
3841
func (l *ListFlag) Set(value string) error {
3942
if l.values == nil {
4043
l.values = &[]string{}
4144
}
4245
items := strings.Split(value, ",")
43-
*l.values = append(*l.values, items...)
46+
if PolicyListAppend {
47+
*l.values = append(*l.values, items...)
48+
}
4449
return nil
4550
}

source.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package figtree
2+
3+
type SourceKind int
4+
5+
const (
6+
SourceKindUnknown SourceKind = iota
7+
SourceKindEnv
8+
SourceKindFile
9+
SourceKindFlag
10+
SourceKindFlagEnv
11+
)
12+
13+
type SourceConfig interface {
14+
Fetch() (string, error)
15+
Kind() SourceKind
16+
}
17+
18+
func (tree *figTree) WithSource(source SourceConfig) error {
19+
return nil
20+
}
21+
22+
func (tree *figTree) Source(name string) error {
23+
tree.mu.RLock()
24+
source, exists := tree.sources[name]
25+
tree.mu.RUnlock()
26+
if !exists {
27+
return ErrSourceNotFound{}
28+
}
29+
result, err := source.Fetch()
30+
if err != nil {
31+
return err
32+
}
33+
tree.StoreString(name, result)
34+
35+
return nil
36+
}
37+
38+
type ErrSourceNotFound struct{}
39+
40+
func (e ErrSourceNotFound) Error() string {
41+
return "source not found"
42+
}

types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ type Plant interface {
2626
// Fig returns a figFruit from the figTree by its name
2727
Fig(name string) Flesh
2828

29+
// Source runs the attached WithSource against the SourceConfig
30+
Source(name string) error
31+
2932
// WithValidator binds a figValidatorFunc to a figFruit that returns Plant
3033
WithValidator(name string, validator func(interface{}) error) Plant
3134

@@ -142,6 +145,8 @@ type figTree struct {
142145
pollinate bool
143146
figs map[string]*figFruit
144147
withered map[string]witheredFig
148+
sources map[string]SourceConfig
149+
sourceLocker sync.RWMutex
145150
mu sync.RWMutex
146151
tracking bool
147152
problems []error

0 commit comments

Comments
 (0)