map_input_source.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package altsrc
  2. import (
  3. "fmt"
  4. "reflect"
  5. "strings"
  6. "time"
  7. "gopkg.in/urfave/cli.v1"
  8. )
  9. // MapInputSource implements InputSourceContext to return
  10. // data from the map that is loaded.
  11. type MapInputSource struct {
  12. valueMap map[interface{}]interface{}
  13. }
  14. // nestedVal checks if the name has '.' delimiters.
  15. // If so, it tries to traverse the tree by the '.' delimited sections to find
  16. // a nested value for the key.
  17. func nestedVal(name string, tree map[interface{}]interface{}) (interface{}, bool) {
  18. if sections := strings.Split(name, "."); len(sections) > 1 {
  19. node := tree
  20. for _, section := range sections[:len(sections)-1] {
  21. if child, ok := node[section]; !ok {
  22. return nil, false
  23. } else {
  24. if ctype, ok := child.(map[interface{}]interface{}); !ok {
  25. return nil, false
  26. } else {
  27. node = ctype
  28. }
  29. }
  30. }
  31. if val, ok := node[sections[len(sections)-1]]; ok {
  32. return val, true
  33. }
  34. }
  35. return nil, false
  36. }
  37. // Int returns an int from the map if it exists otherwise returns 0
  38. func (fsm *MapInputSource) Int(name string) (int, error) {
  39. otherGenericValue, exists := fsm.valueMap[name]
  40. if exists {
  41. otherValue, isType := otherGenericValue.(int)
  42. if !isType {
  43. return 0, incorrectTypeForFlagError(name, "int", otherGenericValue)
  44. }
  45. return otherValue, nil
  46. }
  47. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  48. if exists {
  49. otherValue, isType := nestedGenericValue.(int)
  50. if !isType {
  51. return 0, incorrectTypeForFlagError(name, "int", nestedGenericValue)
  52. }
  53. return otherValue, nil
  54. }
  55. return 0, nil
  56. }
  57. // Duration returns a duration from the map if it exists otherwise returns 0
  58. func (fsm *MapInputSource) Duration(name string) (time.Duration, error) {
  59. otherGenericValue, exists := fsm.valueMap[name]
  60. if exists {
  61. otherValue, isType := otherGenericValue.(time.Duration)
  62. if !isType {
  63. return 0, incorrectTypeForFlagError(name, "duration", otherGenericValue)
  64. }
  65. return otherValue, nil
  66. }
  67. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  68. if exists {
  69. otherValue, isType := nestedGenericValue.(time.Duration)
  70. if !isType {
  71. return 0, incorrectTypeForFlagError(name, "duration", nestedGenericValue)
  72. }
  73. return otherValue, nil
  74. }
  75. return 0, nil
  76. }
  77. // Float64 returns an float64 from the map if it exists otherwise returns 0
  78. func (fsm *MapInputSource) Float64(name string) (float64, error) {
  79. otherGenericValue, exists := fsm.valueMap[name]
  80. if exists {
  81. otherValue, isType := otherGenericValue.(float64)
  82. if !isType {
  83. return 0, incorrectTypeForFlagError(name, "float64", otherGenericValue)
  84. }
  85. return otherValue, nil
  86. }
  87. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  88. if exists {
  89. otherValue, isType := nestedGenericValue.(float64)
  90. if !isType {
  91. return 0, incorrectTypeForFlagError(name, "float64", nestedGenericValue)
  92. }
  93. return otherValue, nil
  94. }
  95. return 0, nil
  96. }
  97. // String returns a string from the map if it exists otherwise returns an empty string
  98. func (fsm *MapInputSource) String(name string) (string, error) {
  99. otherGenericValue, exists := fsm.valueMap[name]
  100. if exists {
  101. otherValue, isType := otherGenericValue.(string)
  102. if !isType {
  103. return "", incorrectTypeForFlagError(name, "string", otherGenericValue)
  104. }
  105. return otherValue, nil
  106. }
  107. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  108. if exists {
  109. otherValue, isType := nestedGenericValue.(string)
  110. if !isType {
  111. return "", incorrectTypeForFlagError(name, "string", nestedGenericValue)
  112. }
  113. return otherValue, nil
  114. }
  115. return "", nil
  116. }
  117. // StringSlice returns an []string from the map if it exists otherwise returns nil
  118. func (fsm *MapInputSource) StringSlice(name string) ([]string, error) {
  119. otherGenericValue, exists := fsm.valueMap[name]
  120. if !exists {
  121. otherGenericValue, exists = nestedVal(name, fsm.valueMap)
  122. if !exists {
  123. return nil, nil
  124. }
  125. }
  126. otherValue, isType := otherGenericValue.([]interface{})
  127. if !isType {
  128. return nil, incorrectTypeForFlagError(name, "[]interface{}", otherGenericValue)
  129. }
  130. var stringSlice = make([]string, 0, len(otherValue))
  131. for i, v := range otherValue {
  132. stringValue, isType := v.(string)
  133. if !isType {
  134. return nil, incorrectTypeForFlagError(fmt.Sprintf("%s[%d]", name, i), "string", v)
  135. }
  136. stringSlice = append(stringSlice, stringValue)
  137. }
  138. return stringSlice, nil
  139. }
  140. // IntSlice returns an []int from the map if it exists otherwise returns nil
  141. func (fsm *MapInputSource) IntSlice(name string) ([]int, error) {
  142. otherGenericValue, exists := fsm.valueMap[name]
  143. if !exists {
  144. otherGenericValue, exists = nestedVal(name, fsm.valueMap)
  145. if !exists {
  146. return nil, nil
  147. }
  148. }
  149. otherValue, isType := otherGenericValue.([]interface{})
  150. if !isType {
  151. return nil, incorrectTypeForFlagError(name, "[]interface{}", otherGenericValue)
  152. }
  153. var intSlice = make([]int, 0, len(otherValue))
  154. for i, v := range otherValue {
  155. intValue, isType := v.(int)
  156. if !isType {
  157. return nil, incorrectTypeForFlagError(fmt.Sprintf("%s[%d]", name, i), "int", v)
  158. }
  159. intSlice = append(intSlice, intValue)
  160. }
  161. return intSlice, nil
  162. }
  163. // Generic returns an cli.Generic from the map if it exists otherwise returns nil
  164. func (fsm *MapInputSource) Generic(name string) (cli.Generic, error) {
  165. otherGenericValue, exists := fsm.valueMap[name]
  166. if exists {
  167. otherValue, isType := otherGenericValue.(cli.Generic)
  168. if !isType {
  169. return nil, incorrectTypeForFlagError(name, "cli.Generic", otherGenericValue)
  170. }
  171. return otherValue, nil
  172. }
  173. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  174. if exists {
  175. otherValue, isType := nestedGenericValue.(cli.Generic)
  176. if !isType {
  177. return nil, incorrectTypeForFlagError(name, "cli.Generic", nestedGenericValue)
  178. }
  179. return otherValue, nil
  180. }
  181. return nil, nil
  182. }
  183. // Bool returns an bool from the map otherwise returns false
  184. func (fsm *MapInputSource) Bool(name string) (bool, error) {
  185. otherGenericValue, exists := fsm.valueMap[name]
  186. if exists {
  187. otherValue, isType := otherGenericValue.(bool)
  188. if !isType {
  189. return false, incorrectTypeForFlagError(name, "bool", otherGenericValue)
  190. }
  191. return otherValue, nil
  192. }
  193. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  194. if exists {
  195. otherValue, isType := nestedGenericValue.(bool)
  196. if !isType {
  197. return false, incorrectTypeForFlagError(name, "bool", nestedGenericValue)
  198. }
  199. return otherValue, nil
  200. }
  201. return false, nil
  202. }
  203. // BoolT returns an bool from the map otherwise returns true
  204. func (fsm *MapInputSource) BoolT(name string) (bool, error) {
  205. otherGenericValue, exists := fsm.valueMap[name]
  206. if exists {
  207. otherValue, isType := otherGenericValue.(bool)
  208. if !isType {
  209. return true, incorrectTypeForFlagError(name, "bool", otherGenericValue)
  210. }
  211. return otherValue, nil
  212. }
  213. nestedGenericValue, exists := nestedVal(name, fsm.valueMap)
  214. if exists {
  215. otherValue, isType := nestedGenericValue.(bool)
  216. if !isType {
  217. return true, incorrectTypeForFlagError(name, "bool", nestedGenericValue)
  218. }
  219. return otherValue, nil
  220. }
  221. return true, nil
  222. }
  223. func incorrectTypeForFlagError(name, expectedTypeName string, value interface{}) error {
  224. valueType := reflect.TypeOf(value)
  225. valueTypeName := ""
  226. if valueType != nil {
  227. valueTypeName = valueType.Name()
  228. }
  229. return fmt.Errorf("Mismatched type for flag '%s'. Expected '%s' but actual is '%s'", name, expectedTypeName, valueTypeName)
  230. }