Skip to content

Commit 9bfcfb9

Browse files
Merge pull request #10 from andreimerlescu/develop
Added WithAlias to Figtree
2 parents ce21143 + 215ee19 commit 9bfcfb9

37 files changed

+2702
-823
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ figs := figtree.With(Options{Tracking: true, Harvest: 1776, Pollinate: true})
203203
figs.NewString(kDomain, "", "Domain name")
204204
figs.WithValidator(kDomain, figtree.AssureStringLengthGreaterThan(3))
205205
figs.WithValidator(kDomain, figtree.AssureStringHasPrefix("https://"))
206-
figs.WithCallback(kDomain, figree.CallbackAfterVerify, func(value interface{}) error {
206+
figs.WithCallback(kDomain, figtree.CallbackAfterVerify, func(value interface{}) error {
207207
var s string
208208
switch v := value.(type) {
209209
case *string:
@@ -214,7 +214,7 @@ figs.WithCallback(kDomain, figree.CallbackAfterVerify, func(value interface{}) e
214214
// try connecting to the domain now
215215
return CheckAvailability(s)
216216
})
217-
figs.WithCallback(kDomain, figree.CallbackAfterRead, func(value interface{}) error {
217+
figs.WithCallback(kDomain, figtree.CallbackAfterRead, func(value interface{}) error {
218218
// every time *figs.String(kDomain) is called, run this
219219
var s string
220220
switch v := value.(type) {

VERSION

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

alias.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
11
package figtree
22

3-
func (tree *figTree) WithAlias(name, alias string) {
3+
import (
4+
"flag"
5+
"fmt"
6+
"strings"
7+
)
8+
9+
func (tree *figTree) WithAlias(name, alias string) Plant {
410
tree.mu.Lock()
511
defer tree.mu.Unlock()
12+
name = strings.ToLower(name)
13+
alias = strings.ToLower(alias)
614
if _, exists := tree.aliases[alias]; exists {
7-
return
15+
return tree
816
}
917
tree.aliases[alias] = name
18+
ptr, ok := tree.values.Load(name)
19+
if !ok {
20+
fmt.Println("failed to load -" + name + " value")
21+
return tree
22+
}
23+
value, ok := ptr.(*Value)
24+
if !ok {
25+
fmt.Println("failed to cast -" + name + " value")
26+
return tree
27+
}
28+
flag.Var(value, alias, "Alias of -"+name)
29+
return tree
1030
}

alias_test.go

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package figtree
22

33
import (
4+
"os"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -11,26 +12,30 @@ func TestWithAlias(t *testing.T) {
1112
const cmdShort, cmdAliasShort, valueShort = "short", "s", "default"
1213

1314
t.Run("basic_usage", func(t *testing.T) {
15+
os.Args = []string{os.Args[0], "-l", t.Name()}
1416
figs := With(Options{Germinate: true, Tracking: false})
15-
figs.NewString(cmdLong, valueLong, usage)
16-
figs.WithAlias(cmdLong, cmdAliasLong)
17+
figs = figs.NewString(cmdLong, valueLong, usage)
18+
figs = figs.WithAlias(cmdLong, cmdAliasLong)
1719
assert.NoError(t, figs.Parse())
1820

19-
assert.Equal(t, valueLong, *figs.String(cmdLong))
20-
assert.Equal(t, valueLong, *figs.String(cmdAliasLong))
21+
assert.NotEqual(t, valueLong, *figs.String(cmdLong))
22+
assert.Equal(t, t.Name(), *figs.String(cmdLong))
23+
assert.NotEqual(t, valueLong, *figs.String(cmdAliasLong))
24+
assert.Equal(t, t.Name(), *figs.String(cmdAliasLong))
2125
figs = nil
2226
})
2327

2428
t.Run("multiple_aliases", func(t *testing.T) {
29+
os.Args = []string{os.Args[0]}
2530
const k, v, u = "name", "yeshua", "the real name of god"
2631
ka1 := "father"
2732
ka2 := "son"
2833
ka3 := "rauch-hokadesch"
2934
figs := With(Options{Germinate: true, Tracking: false})
30-
figs.NewString(k, v, u)
31-
figs.WithAlias(k, ka1)
32-
figs.WithAlias(k, ka2)
33-
figs.WithAlias(k, ka3)
35+
figs = figs.NewString(k, v, u)
36+
figs = figs.WithAlias(k, ka1)
37+
figs = figs.WithAlias(k, ka2)
38+
figs = figs.WithAlias(k, ka3)
3439
assert.NoError(t, figs.Parse())
3540

3641
assert.Equal(t, v, *figs.String(k))
@@ -41,17 +46,17 @@ func TestWithAlias(t *testing.T) {
4146
})
4247

4348
t.Run("complex_usage", func(t *testing.T) {
44-
49+
os.Args = []string{os.Args[0]}
4550
figs := With(Options{Germinate: true, Tracking: false})
4651
// long
47-
figs.NewString(cmdLong, valueLong, usage)
48-
figs.WithAlias(cmdLong, cmdAliasLong)
49-
figs.WithValidator(cmdLong, AssureStringNotEmpty)
52+
figs = figs.NewString(cmdLong, valueLong, usage)
53+
figs = figs.WithAlias(cmdLong, cmdAliasLong)
54+
figs = figs.WithValidator(cmdLong, AssureStringNotEmpty)
5055

5156
// short
52-
figs.NewString(cmdShort, valueShort, usage)
53-
figs.WithAlias(cmdShort, cmdAliasShort)
54-
figs.WithValidator(cmdShort, AssureStringNotEmpty)
57+
figs = figs.NewString(cmdShort, valueShort, usage)
58+
figs = figs.WithAlias(cmdShort, cmdAliasShort)
59+
figs = figs.WithValidator(cmdShort, AssureStringNotEmpty)
5560

5661
assert.NoError(t, figs.Parse())
5762

@@ -63,24 +68,25 @@ func TestWithAlias(t *testing.T) {
6368
assert.Equal(t, valueShort, *figs.String(cmdAliasShort))
6469

6570
figs = nil
66-
6771
})
6872

6973
t.Run("alias_with_int", func(t *testing.T) {
74+
os.Args = []string{os.Args[0]}
7075
figs := With(Options{Germinate: true})
71-
figs.NewInt("count", 42, "usage")
72-
figs.WithAlias("count", "c")
76+
figs = figs.NewInt("count", 42, "usage")
77+
figs = figs.WithAlias("count", "c")
7378
assert.NoError(t, figs.Parse())
7479
assert.Equal(t, 42, *figs.Int("count"))
7580
assert.Equal(t, 42, *figs.Int("c"))
7681
})
7782

7883
t.Run("alias_conflict", func(t *testing.T) {
84+
os.Args = []string{os.Args[0]}
7985
figs := With(Options{Germinate: true})
80-
figs.NewString("one", "value1", "usage")
81-
figs.NewString("two", "value2", "usage")
82-
figs.WithAlias("one", "x")
83-
figs.WithAlias("two", "x") // Should this overwrite or be ignored?
86+
figs = figs.NewString("one", "value1", "usage")
87+
figs = figs.NewString("two", "value2", "usage")
88+
figs = figs.WithAlias("one", "x")
89+
figs = figs.WithAlias("two", "x") // Should this overwrite or be ignored?
8490
assert.NoError(t, figs.Parse())
8591
assert.Equal(t, "value1", *figs.String("x")) // Clarify expected behavior
8692
})

0 commit comments

Comments
 (0)