mapper.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. package mapper
  2. import (
  3. "strconv"
  4. "strings"
  5. )
  6. //--------------------------------------------------------------------------
  7. // Contracts
  8. //--------------------------------------------------------------------------
  9. // Getter is an interface that retrieves a string value for a string key
  10. type Getter interface {
  11. Get(key string) string
  12. }
  13. // Setter is an interface that sets the value of a string key to a string value.
  14. type Setter interface {
  15. Set(key string, value string) error
  16. }
  17. // Map is an interface that gets and sets the value of string keys to/from
  18. // string values
  19. type Map interface {
  20. Getter
  21. Setter
  22. }
  23. // PrimitiveMapReader is an implementation contract for an object capable
  24. // of reading primitive values from a util.Map
  25. type PrimitiveMapReader interface {
  26. // Get parses an string from the map key parameter. If the value
  27. // is empty, the defaultValue parameter is returned.
  28. Get(key string, defaultValue string) string
  29. // GetInt parses an int from the map key parameter. If the value
  30. // is empty or fails to parse, the defaultValue parameter is returned.
  31. GetInt(key string, defaultValue int) int
  32. // GetInt8 parses an int8 from the map key parameter. If the value
  33. // is empty or fails to parse, the defaultValue parameter is returned.
  34. GetInt8(key string, defaultValue int8) int8
  35. // GetInt16 parses an int16 from the map key parameter. If the value
  36. // is empty or fails to parse, the defaultValue parameter is returned.
  37. GetInt16(key string, defaultValue int16) int16
  38. // GetInt32 parses an int32 from the map key parameter. If the value
  39. // is empty or fails to parse, the defaultValue parameter is returned.
  40. GetInt32(key string, defaultValue int32) int32
  41. // GetInt64 parses an int64 from the map key parameter. If the value
  42. // is empty or fails to parse, the defaultValue parameter is returned.
  43. GetInt64(key string, defaultValue int64) int64
  44. // GetUInt parses a uint from the map key parameter. If the value
  45. // is empty or fails to parse, the defaultValue parameter is returned.
  46. GetUInt(key string, defaultValue uint) uint
  47. // GetUInt8 parses a uint8 from the map key parameter. If the value
  48. // is empty or fails to parse, the defaultValue parameter is returned.
  49. GetUInt8(key string, defaultValue uint8) uint8
  50. // GetUInt16 parses a uint16 from the map key parameter. If the value
  51. // is empty or fails to parse, the defaultValue parameter is returned.
  52. GetUInt16(key string, defaultValue uint16) uint16
  53. // GetUInt32 parses a uint32 from the map key parameter. If the value
  54. // is empty or fails to parse, the defaultValue parameter is returned.
  55. GetUInt32(key string, defaultValue uint32) uint32
  56. // GetUInt64 parses a uint64 from the map key parameter. If the value
  57. // is empty or fails to parse, the defaultValue parameter is returned.
  58. GetUInt64(key string, defaultValue uint64) uint64
  59. // GetFloat32 parses a float32 from the map key parameter. If the value
  60. // is empty or fails to parse, the defaultValue parameter is returned.
  61. GetFloat32(key string, defaultValue float32) float32
  62. // GetFloat64 parses a float64 from the map key parameter. If the value
  63. // is empty or fails to parse, the defaultValue parameter is returned.
  64. GetFloat64(key string, defaultValue float64) float64
  65. // GetBool parses a bool from the map key parameter. If the value
  66. // is empty or fails to parse, the defaultValue parameter is returned.
  67. GetBool(key string, defaultValue bool) bool
  68. // GetList returns a string list which contains the value set by key split using the
  69. // provided delimiter with each entry trimmed of space. If the value doesn't exist,
  70. // nil is returned
  71. GetList(key string, delimiter string) []string
  72. }
  73. // PrimitiveMapWriter is an implementation contract for an object capable
  74. // of write primitive values to a util.Map
  75. type PrimitiveMapWriter interface {
  76. // Set sets the map for the key provided using the value provided.
  77. Set(key string, value string) error
  78. // SetInt sets the map to a string formatted int value
  79. SetInt(key string, value int) error
  80. // SetInt8 sets the map to a string formatted int8 value.
  81. SetInt8(key string, value int8) error
  82. // SetInt16 sets the map to a string formatted int16 value.
  83. SetInt16(key string, value int16) error
  84. // SetInt32 sets the map to a string formatted int32 value.
  85. SetInt32(key string, value int32) error
  86. // SetInt64 sets the map to a string formatted int64 value.
  87. SetInt64(key string, value int64) error
  88. // SetUInt sets the map to a string formatted uint value
  89. SetUInt(key string, value uint) error
  90. // SetUInt8 sets the map to a string formatted uint8 value
  91. SetUInt8(key string, value uint8) error
  92. // SetUInt16 sets the map to a string formatted uint16 value
  93. SetUInt16(key string, value uint16) error
  94. // SetUInt32 sets the map to a string formatted uint32 value
  95. SetUInt32(key string, value uint32) error
  96. // SetUInt64 sets the map to a string formatted uint64 value
  97. SetUInt64(key string, value uint64) error
  98. // SetBool sets the map to a string formatted bool value.
  99. SetBool(key string, value bool) error
  100. // SetList sets the map's value at key to a string consistent of each value in the list separated
  101. // by the provided delimiter.
  102. SetList(key string, values []string, delimiter string) error
  103. }
  104. // PrimitiveMap is capable of reading and writing primitive values
  105. // to/from a util.Map
  106. type PrimitiveMap interface {
  107. PrimitiveMapReader
  108. PrimitiveMapWriter
  109. }
  110. //--------------------------------------------------------------------------
  111. // Go Map Implementation
  112. //--------------------------------------------------------------------------
  113. // map[string]string adapter
  114. type goMap struct {
  115. m map[string]string
  116. }
  117. // Get implements mapper.Getter
  118. func (gm *goMap) Get(key string) string {
  119. return gm.m[key]
  120. }
  121. // Set implements mapper.Setter
  122. func (gm *goMap) Set(key, value string) error {
  123. gm.m[key] = value
  124. return nil
  125. }
  126. // NewMap creates a new mapper.Map implementation
  127. func NewMap() Map {
  128. return &goMap{
  129. m: make(map[string]string),
  130. }
  131. }
  132. //--------------------------------------------------------------------------
  133. // PrimitiveMap Implementation
  134. //--------------------------------------------------------------------------
  135. // readOnlyMapper provides Get methods for most primitive go types
  136. type readOnlyMapper struct {
  137. getter Getter
  138. }
  139. // writeOnlyMapper provides Set methods for most primitive go types
  140. type writeOnlyMapper struct {
  141. setter Setter
  142. }
  143. // mapper provides Get and Set methods for most primitive go types
  144. type mapper struct {
  145. *readOnlyMapper
  146. *writeOnlyMapper
  147. }
  148. // NewReadOnlyMapper creates a new implementation of a PrimitiveMapReader
  149. func NewReadOnlyMapper(getter Getter) PrimitiveMapReader {
  150. return &readOnlyMapper{getter}
  151. }
  152. // NewWriteOnlyMapper creates a new implementation of a PrimitiveMapWriter
  153. func NewWriteOnlyMapper(setter Setter) PrimitiveMapWriter {
  154. return &writeOnlyMapper{setter}
  155. }
  156. // NewMapper creates a new implementation of a PrimitiveMap
  157. func NewMapper(m Map) PrimitiveMap {
  158. return &mapper{
  159. readOnlyMapper: &readOnlyMapper{m},
  160. writeOnlyMapper: &writeOnlyMapper{m},
  161. }
  162. }
  163. // NewCompositionMapper creates a new implementation of a PrimitiveMap composed of a
  164. // custom Getter implementation and Setter implementation
  165. func NewCompositionMapper(getter Getter, setter Setter) PrimitiveMap {
  166. return &mapper{
  167. readOnlyMapper: &readOnlyMapper{getter},
  168. writeOnlyMapper: &writeOnlyMapper{setter},
  169. }
  170. }
  171. // Get parses an string from the read-only mapper key parameter. If the value
  172. // is empty, the defaultValue parameter is returned.
  173. func (rom *readOnlyMapper) Get(key string, defaultValue string) string {
  174. r := rom.getter.Get(key)
  175. if r == "" {
  176. return defaultValue
  177. }
  178. return r
  179. }
  180. // GetInt parses an int from the read-only mapper key parameter. If the value
  181. // is empty or fails to parse, the defaultValue parameter is returned.
  182. func (rom *readOnlyMapper) GetInt(key string, defaultValue int) int {
  183. r := rom.getter.Get(key)
  184. i, err := strconv.Atoi(r)
  185. if err != nil {
  186. return defaultValue
  187. }
  188. return i
  189. }
  190. // GetInt8 parses an int8 from the read-only mapper key parameter. If the value
  191. // is empty or fails to parse, the defaultValue parameter is returned.
  192. func (rom *readOnlyMapper) GetInt8(key string, defaultValue int8) int8 {
  193. r := rom.getter.Get(key)
  194. i, err := strconv.ParseInt(r, 10, 8)
  195. if err != nil {
  196. return defaultValue
  197. }
  198. return int8(i)
  199. }
  200. // GetInt16 parses an int16 from the read-only mapper key parameter. If the value
  201. // is empty or fails to parse, the defaultValue parameter is returned.
  202. func (rom *readOnlyMapper) GetInt16(key string, defaultValue int16) int16 {
  203. r := rom.getter.Get(key)
  204. i, err := strconv.ParseInt(r, 10, 16)
  205. if err != nil {
  206. return defaultValue
  207. }
  208. return int16(i)
  209. }
  210. // GetInt32 parses an int32 from the read-only mapper key parameter. If the value
  211. // is empty or fails to parse, the defaultValue parameter is returned.
  212. func (rom *readOnlyMapper) GetInt32(key string, defaultValue int32) int32 {
  213. r := rom.getter.Get(key)
  214. i, err := strconv.ParseInt(r, 10, 32)
  215. if err != nil {
  216. return defaultValue
  217. }
  218. return int32(i)
  219. }
  220. // GetInt64 parses an int64 from the read-only mapper key parameter. If the value
  221. // is empty or fails to parse, the defaultValue parameter is returned.
  222. func (rom *readOnlyMapper) GetInt64(key string, defaultValue int64) int64 {
  223. r := rom.getter.Get(key)
  224. i, err := strconv.ParseInt(r, 10, 64)
  225. if err != nil {
  226. return defaultValue
  227. }
  228. return i
  229. }
  230. // GetUInt parses a uint from the read-only mapper key parameter. If the value
  231. // is empty or fails to parse, the defaultValue parameter is returned.
  232. func (rom *readOnlyMapper) GetUInt(key string, defaultValue uint) uint {
  233. r := rom.getter.Get(key)
  234. i, err := strconv.ParseUint(r, 10, 32)
  235. if err != nil {
  236. return defaultValue
  237. }
  238. return uint(i)
  239. }
  240. // GetUInt8 parses a uint8 from the read-only mapper key parameter. If the value
  241. // is empty or fails to parse, the defaultValue parameter is returned.
  242. func (rom *readOnlyMapper) GetUInt8(key string, defaultValue uint8) uint8 {
  243. r := rom.getter.Get(key)
  244. i, err := strconv.ParseUint(r, 10, 8)
  245. if err != nil {
  246. return defaultValue
  247. }
  248. return uint8(i)
  249. }
  250. // GetUInt16 parses a uint16 from the read-only mapper key parameter. If the value
  251. // is empty or fails to parse, the defaultValue parameter is returned.
  252. func (rom *readOnlyMapper) GetUInt16(key string, defaultValue uint16) uint16 {
  253. r := rom.getter.Get(key)
  254. i, err := strconv.ParseUint(r, 10, 16)
  255. if err != nil {
  256. return defaultValue
  257. }
  258. return uint16(i)
  259. }
  260. // GetUInt32 parses a uint32 from the read-only mapper key parameter. If the value
  261. // is empty or fails to parse, the defaultValue parameter is returned.
  262. func (rom *readOnlyMapper) GetUInt32(key string, defaultValue uint32) uint32 {
  263. r := rom.getter.Get(key)
  264. i, err := strconv.ParseUint(r, 10, 32)
  265. if err != nil {
  266. return defaultValue
  267. }
  268. return uint32(i)
  269. }
  270. // GetUInt64 parses a uint64 from the read-only mapper key parameter. If the value
  271. // is empty or fails to parse, the defaultValue parameter is returned.
  272. func (rom *readOnlyMapper) GetUInt64(key string, defaultValue uint64) uint64 {
  273. r := rom.getter.Get(key)
  274. i, err := strconv.ParseUint(r, 10, 64)
  275. if err != nil {
  276. return defaultValue
  277. }
  278. return uint64(i)
  279. }
  280. // GetFloat32 parses a float32 from the read-only mapper key parameter. If the value
  281. // is empty or fails to parse, the defaultValue parameter is returned.
  282. func (rom *readOnlyMapper) GetFloat32(key string, defaultValue float32) float32 {
  283. r := rom.getter.Get(key)
  284. f, err := strconv.ParseFloat(r, 32)
  285. if err != nil {
  286. return defaultValue
  287. }
  288. return float32(f)
  289. }
  290. // GetFloat64 parses a float64 from the read-only mapper key parameter. If the value
  291. // is empty or fails to parse, the defaultValue parameter is returned.
  292. func (rom *readOnlyMapper) GetFloat64(key string, defaultValue float64) float64 {
  293. r := rom.getter.Get(key)
  294. f, err := strconv.ParseFloat(r, 64)
  295. if err != nil {
  296. return defaultValue
  297. }
  298. return f
  299. }
  300. // GetBool parses a bool from the read-only mapper key parameter. If the value
  301. // is empty or fails to parse, the defaultValue parameter is returned.
  302. func (rom *readOnlyMapper) GetBool(key string, defaultValue bool) bool {
  303. r := rom.getter.Get(key)
  304. b, err := strconv.ParseBool(r)
  305. if err != nil {
  306. return defaultValue
  307. }
  308. return b
  309. }
  310. // GetList returns a string list which contains the value set by key split using the
  311. // provided delimiter with each entry trimmed of space. If the value doesn't exist,
  312. // nil is returned
  313. func (rom *readOnlyMapper) GetList(key string, delimiter string) []string {
  314. value := rom.Get(key, "")
  315. if value == "" {
  316. return nil
  317. }
  318. split := strings.Split(value, delimiter)
  319. // reuse slice created for split
  320. result := split[:0]
  321. for _, v := range split {
  322. if trimmed := strings.TrimSpace(v); trimmed != "" {
  323. result = append(result, trimmed)
  324. }
  325. }
  326. return result
  327. }
  328. // Set sets the map for the key provided using the value provided.
  329. func (wom *writeOnlyMapper) Set(key string, value string) error {
  330. return wom.setter.Set(key, value)
  331. }
  332. // SetInt sets the map to a string formatted int value
  333. func (wom *writeOnlyMapper) SetInt(key string, value int) error {
  334. return wom.setter.Set(key, strconv.Itoa(value))
  335. }
  336. // SetInt8 sets the map to a string formatted int8 value.
  337. func (wom *writeOnlyMapper) SetInt8(key string, value int8) error {
  338. return wom.setter.Set(key, strconv.FormatInt(int64(value), 10))
  339. }
  340. // SetInt16 sets the map to a string formatted int16 value.
  341. func (wom *writeOnlyMapper) SetInt16(key string, value int16) error {
  342. return wom.setter.Set(key, strconv.FormatInt(int64(value), 10))
  343. }
  344. // SetInt32 sets the map to a string formatted int32 value.
  345. func (wom *writeOnlyMapper) SetInt32(key string, value int32) error {
  346. return wom.setter.Set(key, strconv.FormatInt(int64(value), 10))
  347. }
  348. // SetInt64 sets the map to a string formatted int64 value.
  349. func (wom *writeOnlyMapper) SetInt64(key string, value int64) error {
  350. return wom.setter.Set(key, strconv.FormatInt(value, 10))
  351. }
  352. // SetUInt sets the map to a string formatted uint value
  353. func (wom *writeOnlyMapper) SetUInt(key string, value uint) error {
  354. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  355. }
  356. // SetUInt8 sets the map to a string formatted uint8 value
  357. func (wom *writeOnlyMapper) SetUInt8(key string, value uint8) error {
  358. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  359. }
  360. // SetUInt16 sets the map to a string formatted uint16 value
  361. func (wom *writeOnlyMapper) SetUInt16(key string, value uint16) error {
  362. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  363. }
  364. // SetUInt32 sets the map to a string formatted uint32 value
  365. func (wom *writeOnlyMapper) SetUInt32(key string, value uint32) error {
  366. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  367. }
  368. // SetUInt64 sets the map to a string formatted uint64 value
  369. func (wom *writeOnlyMapper) SetUInt64(key string, value uint64) error {
  370. return wom.setter.Set(key, strconv.FormatUint(value, 10))
  371. }
  372. // SetBool sets the map to a string formatted bool value.
  373. func (wom *writeOnlyMapper) SetBool(key string, value bool) error {
  374. return wom.setter.Set(key, strconv.FormatBool(value))
  375. }
  376. // SetList sets the map's value at key to a string consistent of each value in the list separated
  377. // by the provided delimiter.
  378. func (wom *writeOnlyMapper) SetList(key string, values []string, delimiter string) error {
  379. return wom.setter.Set(key, strings.Join(values, delimiter))
  380. }