mapper.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. package mapper
  2. import (
  3. "strconv"
  4. "strings"
  5. "time"
  6. "github.com/kubecost/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. // map[string]string adapter
  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. // NewMap creates a new mapper.Map implementation
  134. func NewMap() Map {
  135. return &goMap{
  136. m: make(map[string]string),
  137. }
  138. }
  139. //--------------------------------------------------------------------------
  140. // PrimitiveMap Implementation
  141. //--------------------------------------------------------------------------
  142. // readOnlyMapper provides Get methods for most primitive go types
  143. type readOnlyMapper struct {
  144. getter Getter
  145. }
  146. // writeOnlyMapper provides Set methods for most primitive go types
  147. type writeOnlyMapper struct {
  148. setter Setter
  149. }
  150. // mapper provides Get and Set methods for most primitive go types
  151. type mapper struct {
  152. *readOnlyMapper
  153. *writeOnlyMapper
  154. }
  155. // NewReadOnlyMapper creates a new implementation of a PrimitiveMapReader
  156. func NewReadOnlyMapper(getter Getter) PrimitiveMapReader {
  157. return &readOnlyMapper{getter}
  158. }
  159. // NewWriteOnlyMapper creates a new implementation of a PrimitiveMapWriter
  160. func NewWriteOnlyMapper(setter Setter) PrimitiveMapWriter {
  161. return &writeOnlyMapper{setter}
  162. }
  163. // NewMapper creates a new implementation of a PrimitiveMap
  164. func NewMapper(m Map) PrimitiveMap {
  165. return &mapper{
  166. readOnlyMapper: &readOnlyMapper{m},
  167. writeOnlyMapper: &writeOnlyMapper{m},
  168. }
  169. }
  170. // NewCompositionMapper creates a new implementation of a PrimitiveMap composed of a
  171. // custom Getter implementation and Setter implementation
  172. func NewCompositionMapper(getter Getter, setter Setter) PrimitiveMap {
  173. return &mapper{
  174. readOnlyMapper: &readOnlyMapper{getter},
  175. writeOnlyMapper: &writeOnlyMapper{setter},
  176. }
  177. }
  178. // Get parses an string from the read-only mapper key parameter. If the value
  179. // is empty, the defaultValue parameter is returned.
  180. func (rom *readOnlyMapper) Get(key string, defaultValue string) string {
  181. r := rom.getter.Get(key)
  182. if r == "" {
  183. return defaultValue
  184. }
  185. return r
  186. }
  187. // GetInt parses an int from the read-only mapper key parameter. If the value
  188. // is empty or fails to parse, the defaultValue parameter is returned.
  189. func (rom *readOnlyMapper) GetInt(key string, defaultValue int) int {
  190. r := rom.getter.Get(key)
  191. i, err := strconv.Atoi(r)
  192. if err != nil {
  193. return defaultValue
  194. }
  195. return i
  196. }
  197. // GetInt8 parses an int8 from the read-only mapper key parameter. If the value
  198. // is empty or fails to parse, the defaultValue parameter is returned.
  199. func (rom *readOnlyMapper) GetInt8(key string, defaultValue int8) int8 {
  200. r := rom.getter.Get(key)
  201. i, err := strconv.ParseInt(r, 10, 8)
  202. if err != nil {
  203. return defaultValue
  204. }
  205. return int8(i)
  206. }
  207. // GetInt16 parses an int16 from the read-only mapper key parameter. If the value
  208. // is empty or fails to parse, the defaultValue parameter is returned.
  209. func (rom *readOnlyMapper) GetInt16(key string, defaultValue int16) int16 {
  210. r := rom.getter.Get(key)
  211. i, err := strconv.ParseInt(r, 10, 16)
  212. if err != nil {
  213. return defaultValue
  214. }
  215. return int16(i)
  216. }
  217. // GetInt32 parses an int32 from the read-only mapper key parameter. If the value
  218. // is empty or fails to parse, the defaultValue parameter is returned.
  219. func (rom *readOnlyMapper) GetInt32(key string, defaultValue int32) int32 {
  220. r := rom.getter.Get(key)
  221. i, err := strconv.ParseInt(r, 10, 32)
  222. if err != nil {
  223. return defaultValue
  224. }
  225. return int32(i)
  226. }
  227. // GetInt64 parses an int64 from the read-only mapper key parameter. If the value
  228. // is empty or fails to parse, the defaultValue parameter is returned.
  229. func (rom *readOnlyMapper) GetInt64(key string, defaultValue int64) int64 {
  230. r := rom.getter.Get(key)
  231. i, err := strconv.ParseInt(r, 10, 64)
  232. if err != nil {
  233. return defaultValue
  234. }
  235. return i
  236. }
  237. // GetUInt parses a uint from the read-only mapper key parameter. If the value
  238. // is empty or fails to parse, the defaultValue parameter is returned.
  239. func (rom *readOnlyMapper) GetUInt(key string, defaultValue uint) uint {
  240. r := rom.getter.Get(key)
  241. i, err := strconv.ParseUint(r, 10, 32)
  242. if err != nil {
  243. return defaultValue
  244. }
  245. return uint(i)
  246. }
  247. // GetUInt8 parses a uint8 from the read-only mapper key parameter. If the value
  248. // is empty or fails to parse, the defaultValue parameter is returned.
  249. func (rom *readOnlyMapper) GetUInt8(key string, defaultValue uint8) uint8 {
  250. r := rom.getter.Get(key)
  251. i, err := strconv.ParseUint(r, 10, 8)
  252. if err != nil {
  253. return defaultValue
  254. }
  255. return uint8(i)
  256. }
  257. // GetUInt16 parses a uint16 from the read-only mapper key parameter. If the value
  258. // is empty or fails to parse, the defaultValue parameter is returned.
  259. func (rom *readOnlyMapper) GetUInt16(key string, defaultValue uint16) uint16 {
  260. r := rom.getter.Get(key)
  261. i, err := strconv.ParseUint(r, 10, 16)
  262. if err != nil {
  263. return defaultValue
  264. }
  265. return uint16(i)
  266. }
  267. // GetUInt32 parses a uint32 from the read-only mapper key parameter. If the value
  268. // is empty or fails to parse, the defaultValue parameter is returned.
  269. func (rom *readOnlyMapper) GetUInt32(key string, defaultValue uint32) uint32 {
  270. r := rom.getter.Get(key)
  271. i, err := strconv.ParseUint(r, 10, 32)
  272. if err != nil {
  273. return defaultValue
  274. }
  275. return uint32(i)
  276. }
  277. // GetUInt64 parses a uint64 from the read-only mapper key parameter. If the value
  278. // is empty or fails to parse, the defaultValue parameter is returned.
  279. func (rom *readOnlyMapper) GetUInt64(key string, defaultValue uint64) uint64 {
  280. r := rom.getter.Get(key)
  281. i, err := strconv.ParseUint(r, 10, 64)
  282. if err != nil {
  283. return defaultValue
  284. }
  285. return uint64(i)
  286. }
  287. // GetFloat32 parses a float32 from the read-only mapper key parameter. If the value
  288. // is empty or fails to parse, the defaultValue parameter is returned.
  289. func (rom *readOnlyMapper) GetFloat32(key string, defaultValue float32) float32 {
  290. r := rom.getter.Get(key)
  291. f, err := strconv.ParseFloat(r, 32)
  292. if err != nil {
  293. return defaultValue
  294. }
  295. return float32(f)
  296. }
  297. // GetFloat64 parses a float64 from the read-only mapper key parameter. If the value
  298. // is empty or fails to parse, the defaultValue parameter is returned.
  299. func (rom *readOnlyMapper) GetFloat64(key string, defaultValue float64) float64 {
  300. r := rom.getter.Get(key)
  301. f, err := strconv.ParseFloat(r, 64)
  302. if err != nil {
  303. return defaultValue
  304. }
  305. return f
  306. }
  307. // GetBool parses a bool from the read-only mapper key parameter. If the value
  308. // is empty or fails to parse, the defaultValue parameter is returned.
  309. func (rom *readOnlyMapper) GetBool(key string, defaultValue bool) bool {
  310. r := rom.getter.Get(key)
  311. b, err := strconv.ParseBool(r)
  312. if err != nil {
  313. return defaultValue
  314. }
  315. return b
  316. }
  317. // GetDuration parses a time.Duration from the read-only mapper key parameter.
  318. // If the value is empty or fails to parse, the defaultValue parameter is returned.
  319. func (rom *readOnlyMapper) GetDuration(key string, defaultValue time.Duration) time.Duration {
  320. r := rom.getter.Get(key)
  321. d, err := timeutil.ParseDuration(r)
  322. if err != nil {
  323. return defaultValue
  324. }
  325. return d
  326. }
  327. // GetList returns a string list which contains the value set by key split using the
  328. // provided delimiter with each entry trimmed of space. If the value doesn't exist,
  329. // nil is returned
  330. func (rom *readOnlyMapper) GetList(key string, delimiter string) []string {
  331. value := rom.Get(key, "")
  332. if value == "" {
  333. return nil
  334. }
  335. split := strings.Split(value, delimiter)
  336. // reuse slice created for split
  337. result := split[:0]
  338. for _, v := range split {
  339. if trimmed := strings.TrimSpace(v); trimmed != "" {
  340. result = append(result, trimmed)
  341. }
  342. }
  343. return result
  344. }
  345. // Set sets the map for the key provided using the value provided.
  346. func (wom *writeOnlyMapper) Set(key string, value string) error {
  347. return wom.setter.Set(key, value)
  348. }
  349. // SetInt sets the map to a string formatted int value
  350. func (wom *writeOnlyMapper) SetInt(key string, value int) error {
  351. return wom.setter.Set(key, strconv.Itoa(value))
  352. }
  353. // SetInt8 sets the map to a string formatted int8 value.
  354. func (wom *writeOnlyMapper) SetInt8(key string, value int8) error {
  355. return wom.setter.Set(key, strconv.FormatInt(int64(value), 10))
  356. }
  357. // SetInt16 sets the map to a string formatted int16 value.
  358. func (wom *writeOnlyMapper) SetInt16(key string, value int16) error {
  359. return wom.setter.Set(key, strconv.FormatInt(int64(value), 10))
  360. }
  361. // SetInt32 sets the map to a string formatted int32 value.
  362. func (wom *writeOnlyMapper) SetInt32(key string, value int32) error {
  363. return wom.setter.Set(key, strconv.FormatInt(int64(value), 10))
  364. }
  365. // SetInt64 sets the map to a string formatted int64 value.
  366. func (wom *writeOnlyMapper) SetInt64(key string, value int64) error {
  367. return wom.setter.Set(key, strconv.FormatInt(value, 10))
  368. }
  369. // SetUInt sets the map to a string formatted uint value
  370. func (wom *writeOnlyMapper) SetUInt(key string, value uint) error {
  371. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  372. }
  373. // SetUInt8 sets the map to a string formatted uint8 value
  374. func (wom *writeOnlyMapper) SetUInt8(key string, value uint8) error {
  375. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  376. }
  377. // SetUInt16 sets the map to a string formatted uint16 value
  378. func (wom *writeOnlyMapper) SetUInt16(key string, value uint16) error {
  379. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  380. }
  381. // SetUInt32 sets the map to a string formatted uint32 value
  382. func (wom *writeOnlyMapper) SetUInt32(key string, value uint32) error {
  383. return wom.setter.Set(key, strconv.FormatUint(uint64(value), 10))
  384. }
  385. // SetUInt64 sets the map to a string formatted uint64 value
  386. func (wom *writeOnlyMapper) SetUInt64(key string, value uint64) error {
  387. return wom.setter.Set(key, strconv.FormatUint(value, 10))
  388. }
  389. // SetBool sets the map to a string formatted bool value.
  390. func (wom *writeOnlyMapper) SetBool(key string, value bool) error {
  391. return wom.setter.Set(key, strconv.FormatBool(value))
  392. }
  393. // SetDuration sets the map to a string formatted bool value.
  394. func (wom *writeOnlyMapper) SetDuration(key string, value time.Duration) error {
  395. return wom.setter.Set(key, timeutil.DurationString(value))
  396. }
  397. // SetList sets the map's value at key to a string consistent of each value in the list separated
  398. // by the provided delimiter.
  399. func (wom *writeOnlyMapper) SetList(key string, values []string, delimiter string) error {
  400. return wom.setter.Set(key, strings.Join(values, delimiter))
  401. }