CLI and configuration library built especially for the parallelcoin pod
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

parser.go 1.5KB

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