mapper.go 14 KB

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