mapper.go 17 KB

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