2
0

mapper.go 16 KB

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