Browse Source

now loads slots with defaults

master
Loki Verloren 7 months ago
parent
commit
3fe0976618
5 changed files with 122 additions and 7 deletions
  1. 5
    2
      cmd/example/example.go
  2. 6
    2
      cmd/example/main.go
  3. 83
    0
      parser.go
  4. 2
    3
      validators.go
  5. 26
    0
      validators_test.go

+ 5
- 2
cmd/example/example.go View File

@@ -6,13 +6,16 @@ import (
6 6
 
7 7
 type exampleConf struct {
8 8
 	datadir string
9
-
10 9
 }
11 10
 
12 11
 var cfg = exampleConf{
13 12
 	datadir: "/not/the/default/path",
14 13
 }
15 14
 
15
+var cfg2 = exampleConf{
16
+	datadir: "/not/the/default/path",
17
+}
18
+
16 19
 // dot import used on tri as it should be for declarations, or every damned name has to have tri. in front of it. This is ok in other places, but not here, as you can obviously see:
17 20
 var exampleTri = Tri{
18 21
 	"appname",
@@ -55,7 +58,7 @@ var exampleTri = Tri{
55 58
 				Help{"help"},
56 59
 				Default{"~/.pod"},
57 60
 				Group{"groupname"},
58
-				Slot{&cfg.datadir},
61
+				Slot{&cfg.datadir, &cfg2.datadir},
59 62
 			},
60 63
 			Trigger{"wallet",
61 64
 				Short{'w'},

+ 6
- 2
cmd/example/main.go View File

@@ -1,10 +1,14 @@
1 1
 package main
2 2
 
3 3
 import (
4
+	"git.parallelcoin.io/tri"
4 5
 	"fmt"
5 6
 )
6 7
 
7 8
 func main() {
8 9
 	e := exampleTri.Validate()
9
-	fmt.Println(e)
10
-}
10
+	if e != nil {
11
+		fmt.Println(e)
12
+	}
13
+	tri.LoadAllDefaults(&exampleTri)
14
+}

+ 83
- 0
parser.go View File

@@ -0,0 +1,83 @@
1
+package tri
2
+
3
+import (
4
+	"time"
5
+)
6
+
7
+// LoadAllDefaults walks a Tri and calls LoadDefaults on each one for the first step in composition of configuration
8
+func LoadAllDefaults(t *Tri) {
9
+	T := *t
10
+	var counter, varsfound int
11
+	for _, x := range T {
12
+		if v, ok := x.(Var); ok {
13
+			varsfound++
14
+			if LoadDefaults(&v) {
15
+				counter++
16
+			}
17
+		}
18
+		if v, ok := x.(Commands); ok {
19
+			for _, x := range v {
20
+				for _, y := range x {
21
+					if v, ok := y.(Var); ok {
22
+						varsfound++
23
+						if LoadDefaults(&v) {
24
+							counter++
25
+						}
26
+					}
27
+				}
28
+			}
29
+		}
30
+	}
31
+}
32
+
33
+// LoadDefaults reads the Default (if any) in a Var, and copies the value into the Slot, returns true if there was a Default and it was filled
34
+func LoadDefaults(v *Var) (found bool) {
35
+	// First find if there is a default
36
+	var def Default
37
+	V := *v
38
+	for _, x := range V {
39
+		if j, ok := x.(Default); ok {
40
+			def = j
41
+			found = true
42
+		}
43
+	}
44
+	if !found {
45
+		return false
46
+	}
47
+	found = false
48
+	var slot Slot
49
+	for _, x := range V {
50
+		if j, ok := x.(Slot); ok {
51
+			slot = j
52
+			found = true
53
+		}
54
+	}
55
+	if !found {
56
+		return false
57
+	}
58
+	for _, x := range slot {
59
+		switch S := x.(type) {
60
+		case *string:
61
+			s := def[0].(string)
62
+			*S = s
63
+		case *int:
64
+			s := def[0].(int)
65
+			*S = s
66
+		case *uint32:
67
+			s := def[0].(uint32)
68
+			*S = s
69
+		case *float64:
70
+			s := def[0].(float64)
71
+			*S = s
72
+		case *[]string:
73
+			s := def[0].([]string)
74
+			*S = s
75
+		case *time.Duration:
76
+			s := def[0].(time.Duration)
77
+			*S = s
78
+		default:
79
+			panic("unrecognised type found in slot")
80
+		}
81
+	}
82
+	return true
83
+}

+ 2
- 3
validators.go View File

@@ -666,13 +666,12 @@ func (r *Var) Validate() error {
666 666
 			for _, z := range R {
667 667
 				s, ok := z.(Slot)
668 668
 				if ok {
669
-					switch S := s[0].(type) {
669
+					switch s[0].(type) {
670 670
 					case *string:
671
-						ss, ok := y[0].(string)
671
+						_, ok := y[0].(string)
672 672
 						if !ok {
673 673
 							return errors.New("slot is not same type as default")
674 674
 						}
675
-						*S = ss
676 675
 					case *int:
677 676
 						_, ok := y[0].(int)
678 677
 						if !ok {

+ 26
- 0
validators_test.go View File

@@ -1,6 +1,7 @@
1 1
 package tri
2 2
 
3 3
 import (
4
+	"time"
4 5
 	"testing"
5 6
 )
6 7
 
@@ -912,6 +913,31 @@ func TestVar(t *testing.T) {
912 913
 	if e := tv20.Validate(); e == nil {
913 914
 		t.Error("validator allowed default that can't be assigned to Slot")
914 915
 	}
916
+	var tint int
917
+	tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tint}, Default{" "}}
918
+	if e := tv20.Validate(); e == nil {
919
+		t.Error("validator allowed default that can't be assigned to Slot")
920
+	}
921
+	var tuint32 uint32
922
+	tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tuint32}, Default{0.1}}
923
+	if e := tv20.Validate(); e == nil {
924
+		t.Error("validator allowed default that can't be assigned to Slot")
925
+	}
926
+	var tfloat64 float64
927
+	tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tfloat64}, Default{5}}
928
+	if e := tv20.Validate(); e == nil {
929
+		t.Error("validator allowed default that can't be assigned to Slot")
930
+	}
931
+	var tstringslice []string
932
+	tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tstringslice}, Default{"aaa"}}
933
+	if e := tv20.Validate(); e == nil {
934
+		t.Error("validator allowed default that can't be assigned to Slot")
935
+	}
936
+	var tduration time.Duration
937
+	tv20 = Var{"aaaa", Brief{"aaaa"}, Slot{&tduration}, Default{5}}
938
+	if e := tv20.Validate(); e == nil {
939
+		t.Error("validator allowed default that can't be assigned to Slot")
940
+	}
915 941
 	// no error!}
916 942
 	tv21 := Var{"aaaa", Brief{tstring}, Slot{&tstring}}
917 943
 	if e := tv21.Validate(); e != nil {

Loading…
Cancel
Save