pricingmodel_codecs.go 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162
  1. // //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // DO NOT MODIFY
  4. //
  5. // ┻━┻ ︵ヽ(`Д´)ノ︵ ┻━┻
  6. //
  7. //
  8. // This source file was automatically generated by bingen.
  9. //
  10. // //////////////////////////////////////////////////////////////////////////////
  11. // TODO This file was infact modified due to an issue with including aliased string types from 'shared'
  12. package pricingmodel
  13. import (
  14. "fmt"
  15. "io"
  16. "iter"
  17. "os"
  18. "reflect"
  19. "strings"
  20. "sync"
  21. "time"
  22. "unsafe"
  23. "github.com/opencost/opencost/core/pkg/model/shared"
  24. "github.com/opencost/opencost/core/pkg/util"
  25. )
  26. const (
  27. // GeneratorPackageName is the package the generator is targetting
  28. GeneratorPackageName string = "pricingmodel"
  29. )
  30. // BinaryTags represent the formatting tag used for specific optimization features
  31. const (
  32. // BinaryTagStringTable is written and/or read prior to the existence of a string
  33. // table (where each index is encoded as a string entry in the resource
  34. BinaryTagStringTable string = "BGST"
  35. )
  36. const (
  37. // DefaultCodecVersion is used for any resources listed in the Default version set
  38. DefaultCodecVersion uint8 = 1
  39. )
  40. //--------------------------------------------------------------------------
  41. // Configuration
  42. //--------------------------------------------------------------------------
  43. var (
  44. bingenConfigLock sync.RWMutex
  45. bingenConfig *BingenConfiguration = DefaultBingenConfiguration()
  46. )
  47. // BingenConfiguration is used to set any custom configuration in the way files are encoded
  48. // or decoded.
  49. type BingenConfiguration struct {
  50. // FileBackedStringTableEnabled enables the use of file-backed string tables for streaming
  51. // bingen decoding.
  52. FileBackedStringTableEnabled bool
  53. // FileBackedStringTableDir is the directory to write the string table files for reading.
  54. FileBackedStringTableDir string
  55. }
  56. // DefaultBingenConfiguration creates the default implementation of the bingen configuration
  57. // and returns it.
  58. func DefaultBingenConfiguration() *BingenConfiguration {
  59. return &BingenConfiguration{
  60. FileBackedStringTableEnabled: false,
  61. FileBackedStringTableDir: os.TempDir(),
  62. }
  63. }
  64. // ConfigureBingen accepts a new *BingenConfiguration instance which updates the internal decoder
  65. // and encoder behavior.
  66. func ConfigureBingen(config *BingenConfiguration) {
  67. bingenConfigLock.Lock()
  68. defer bingenConfigLock.Unlock()
  69. if config == nil {
  70. config = DefaultBingenConfiguration()
  71. }
  72. bingenConfig = config
  73. }
  74. // IsBingenFileBackedStringTableEnabled accessor for file backed string table configuration
  75. func IsBingenFileBackedStringTableEnabled() bool {
  76. bingenConfigLock.RLock()
  77. defer bingenConfigLock.RUnlock()
  78. return bingenConfig.FileBackedStringTableEnabled
  79. }
  80. // BingenFileBackedStringTableDir returns the directory configured for file backed string tables.
  81. func BingenFileBackedStringTableDir() string {
  82. bingenConfigLock.RLock()
  83. defer bingenConfigLock.RUnlock()
  84. return bingenConfig.FileBackedStringTableDir
  85. }
  86. //--------------------------------------------------------------------------
  87. // Type Map
  88. //--------------------------------------------------------------------------
  89. // Generated type map for resolving interface implementations to
  90. // to concrete types
  91. var typeMap map[string]reflect.Type = map[string]reflect.Type{
  92. "NodeKey": reflect.TypeFor[NodeKey](),
  93. "NodePricing": reflect.TypeFor[NodePricing](),
  94. "PricingModelSet": reflect.TypeFor[PricingModelSet](),
  95. }
  96. //--------------------------------------------------------------------------
  97. // Type Helpers
  98. //--------------------------------------------------------------------------
  99. // isBinaryTag returns true when the first bytes in the provided binary matches the tag
  100. func isBinaryTag(data []byte, tag string) bool {
  101. if len(data) < len(tag) {
  102. return false
  103. }
  104. return string(data[:len(tag)]) == tag
  105. }
  106. // isReaderBinaryTag is used to peek the header for an io.Reader Buffer
  107. func isReaderBinaryTag(buff *util.Buffer, tag string) bool {
  108. data, err := buff.Peek(len(tag))
  109. if err != nil && err != io.EOF {
  110. panic(fmt.Sprintf("called Peek() on a non buffered reader: %s", err))
  111. }
  112. if len(data) < len(tag) {
  113. return false
  114. }
  115. return string(data[:len(tag)]) == tag
  116. }
  117. // appendBytes combines a and b into a new byte array
  118. func appendBytes(a []byte, b []byte) []byte {
  119. al := len(a)
  120. bl := len(b)
  121. tl := al + bl
  122. // allocate a new byte array for the combined
  123. // use native copy for speedy byte copying
  124. result := make([]byte, tl)
  125. copy(result, a)
  126. copy(result[al:], b)
  127. return result
  128. }
  129. // typeToString determines the basic properties of the type, the qualifier, package path, and
  130. // type name, and returns the qualified type
  131. func typeToString(f interface{}) string {
  132. qual := ""
  133. t := reflect.TypeOf(f)
  134. if t.Kind() == reflect.Ptr {
  135. t = t.Elem()
  136. qual = "*"
  137. }
  138. return fmt.Sprintf("%s%s.%s", qual, t.PkgPath(), t.Name())
  139. }
  140. // resolveType uses the name of a type and returns the package, base type name, and whether
  141. // or not it's a pointer.
  142. func resolveType(t string) (pkg string, name string, isPtr bool) {
  143. isPtr = t[:1] == "*"
  144. if isPtr {
  145. t = t[1:]
  146. }
  147. slashIndex := strings.LastIndex(t, "/")
  148. if slashIndex >= 0 {
  149. t = t[slashIndex+1:]
  150. }
  151. parts := strings.Split(t, ".")
  152. if parts[0] == GeneratorPackageName {
  153. parts[0] = ""
  154. }
  155. pkg = parts[0]
  156. name = parts[1]
  157. return
  158. }
  159. //--------------------------------------------------------------------------
  160. // Stream Helpers
  161. //--------------------------------------------------------------------------
  162. // StreamFactoryFunc is an alias for a func that creates a BingenStream implementation.
  163. type StreamFactoryFunc func(io.Reader) BingenStream
  164. // Generated streamable factory map for finding the specific new stream methods
  165. // by T type
  166. var streamFactoryMap map[reflect.Type]StreamFactoryFunc = map[reflect.Type]StreamFactoryFunc{}
  167. // NewStreamFor accepts an io.Reader, and returns a new BingenStream for the generic T
  168. // type provided _if_ it is a registered bingen type that is annotated as 'streamable'. See
  169. // the streamFactoryMap for generated type listings.
  170. func NewStreamFor[T any](reader io.Reader) (BingenStream, error) {
  171. typeKey := reflect.TypeFor[T]()
  172. factory, ok := streamFactoryMap[typeKey]
  173. if !ok {
  174. return nil, fmt.Errorf("the type: %s is not a registered bingen streamable type", typeKey.Name())
  175. }
  176. return factory(reader), nil
  177. }
  178. // BingenStream is the stream interface for all streamable types
  179. type BingenStream interface {
  180. // Stream returns the iterator which will stream each field of the target type and
  181. // return the field info as well as the value.
  182. Stream() iter.Seq2[BingenFieldInfo, *BingenValue]
  183. // Close will close any dynamic io.Reader used to stream in the fields
  184. Close()
  185. // Error returns an error if one occurred during the process of streaming the type's fields.
  186. // This can be checked after iterating through the Stream().
  187. Error() error
  188. }
  189. // BingenValue contains the value of a field as well as any index/key associated with that value.
  190. type BingenValue struct {
  191. Value any
  192. Index any
  193. }
  194. // IsNil is just a method accessor way to check to see if the value returned was nil
  195. func (bv *BingenValue) IsNil() bool {
  196. return bv == nil
  197. }
  198. // creates a single BingenValue instance without a key or index
  199. func singleV(value any) *BingenValue {
  200. return &BingenValue{
  201. Value: value,
  202. }
  203. }
  204. // creates a pair of key/index and value.
  205. func pairV(index any, value any) *BingenValue {
  206. return &BingenValue{
  207. Value: value,
  208. Index: index,
  209. }
  210. }
  211. // BingenFieldInfo contains the type of the field being streamed as well as the name of the field.
  212. type BingenFieldInfo struct {
  213. Type reflect.Type
  214. Name string
  215. }
  216. //--------------------------------------------------------------------------
  217. // String Table Writer
  218. //--------------------------------------------------------------------------
  219. // StringTableWriter maps strings to specific indices for encoding
  220. type StringTableWriter struct {
  221. l sync.Mutex
  222. indices map[string]int
  223. next int
  224. }
  225. // NewStringTableWriter Creates a new StringTableWriter instance with provided contents
  226. func NewStringTableWriter(contents ...string) *StringTableWriter {
  227. st := &StringTableWriter{
  228. indices: make(map[string]int, len(contents)),
  229. next: len(contents),
  230. }
  231. for i, entry := range contents {
  232. st.indices[entry] = i
  233. }
  234. return st
  235. }
  236. // AddOrGet atomically retrieves a string entry's index if it exist. Otherwise, it will
  237. // add the entry and return the index.
  238. func (st *StringTableWriter) AddOrGet(s string) int {
  239. st.l.Lock()
  240. defer st.l.Unlock()
  241. if ind, ok := st.indices[s]; ok {
  242. return ind
  243. }
  244. current := st.next
  245. st.next++
  246. st.indices[s] = current
  247. return current
  248. }
  249. // ToSlice Converts the contents to a string array for encoding.
  250. func (st *StringTableWriter) ToSlice() []string {
  251. st.l.Lock()
  252. defer st.l.Unlock()
  253. if st.next == 0 {
  254. return []string{}
  255. }
  256. sl := make([]string, st.next)
  257. for s, i := range st.indices {
  258. sl[i] = s
  259. }
  260. return sl
  261. }
  262. // ToBytes Converts the contents to a binary encoded representation
  263. func (st *StringTableWriter) ToBytes() []byte {
  264. buff := util.NewBuffer()
  265. buff.WriteBytes([]byte(BinaryTagStringTable)) // bingen table header
  266. strs := st.ToSlice()
  267. buff.WriteInt(len(strs)) // table length
  268. for _, s := range strs {
  269. buff.WriteString(s)
  270. }
  271. return buff.Bytes()
  272. }
  273. //--------------------------------------------------------------------------
  274. // String Table Reader
  275. //--------------------------------------------------------------------------
  276. // StringTableReader is the interface used to read the string table from the decoding.
  277. type StringTableReader interface {
  278. // At returns the string entry at a specific index, or panics on out of bounds.
  279. At(index int) string
  280. // Len returns the total number of strings loaded in the string table.
  281. Len() int
  282. // Close will clear the loaded table, and drop any external resources used.
  283. Close() error
  284. }
  285. // SliceStringTableReader is a basic pre-loaded []string that provides index-based access.
  286. // The cost of this implementation is holding all strings in memory, which provides faster
  287. // lookup performance for memory usage.
  288. type SliceStringTableReader struct {
  289. table []string
  290. }
  291. // NewSliceStringTableReaderFrom creates a new SliceStringTableReader instance loading
  292. // data directly from the buffer. The buffer's position should start at the table length.
  293. func NewSliceStringTableReaderFrom(buffer *util.Buffer) StringTableReader {
  294. // table length
  295. tl := buffer.ReadInt()
  296. var table []string
  297. if tl > 0 {
  298. table = make([]string, tl)
  299. for i := range tl {
  300. table[i] = buffer.ReadString()
  301. }
  302. }
  303. return &SliceStringTableReader{
  304. table: table,
  305. }
  306. }
  307. // At returns the string entry at a specific index, or panics on out of bounds.
  308. func (sstr *SliceStringTableReader) At(index int) string {
  309. if index < 0 || index >= len(sstr.table) {
  310. panic(fmt.Errorf("%s: string table index out of bounds: %d", GeneratorPackageName, index))
  311. }
  312. return sstr.table[index]
  313. }
  314. // Len returns the total number of strings loaded in the string table.
  315. func (sstr *SliceStringTableReader) Len() int {
  316. if sstr == nil {
  317. return 0
  318. }
  319. return len(sstr.table)
  320. }
  321. // Close for the slice tables just nils out the slice and returns
  322. func (sstr *SliceStringTableReader) Close() error {
  323. sstr.table = nil
  324. return nil
  325. }
  326. // fileStringRef maps a bingen string-table index to a payload stored in a temp file.
  327. type fileStringRef struct {
  328. off int64
  329. length int
  330. }
  331. // FileStringTableReader leverages a local file to write string table data for lookup. On
  332. // memory focused systems, this allows a slower parse with a significant decrease in memory
  333. // usage. This implementation is often pair with streaming readers for high throughput with
  334. // reduced memory usage.
  335. type FileStringTableReader struct {
  336. f *os.File
  337. refs []fileStringRef
  338. }
  339. // NewFileStringTableFromBuffer reads exactly tl length-prefixed (uint16) string payloads from buffer
  340. // and appends each payload to a new temp file. It does not retain full strings in memory.
  341. func NewFileStringTableReaderFrom(buffer *util.Buffer, dir string) StringTableReader {
  342. // helper func to cast a string in-place to a byte slice.
  343. // NOTE: Return value is READ-ONLY. DO NOT MODIFY!
  344. byteSliceFor := func(s string) []byte {
  345. return unsafe.Slice(unsafe.StringData(s), len(s))
  346. }
  347. err := os.MkdirAll(dir, 0755)
  348. if err != nil {
  349. panic(fmt.Errorf("%s: failed to create string table directory: %w", GeneratorPackageName, err))
  350. }
  351. f, err := os.CreateTemp(dir, fmt.Sprintf("%s-bgst-*", GeneratorPackageName))
  352. if err != nil {
  353. panic(fmt.Errorf("%s: failed to create string table file: %w", GeneratorPackageName, err))
  354. }
  355. var writeErr error
  356. defer func() {
  357. if writeErr != nil {
  358. _ = f.Close()
  359. }
  360. }()
  361. // table length
  362. tl := buffer.ReadInt()
  363. var refs []fileStringRef
  364. if tl > 0 {
  365. refs = make([]fileStringRef, tl)
  366. for i := range tl {
  367. payload := byteSliceFor(buffer.ReadString())
  368. var off int64
  369. if len(payload) > 0 {
  370. off, err = f.Seek(0, io.SeekEnd)
  371. if err != nil {
  372. writeErr = fmt.Errorf("%s: failed to seek string table file: %w", GeneratorPackageName, err)
  373. panic(writeErr)
  374. }
  375. if _, err := f.Write(payload); err != nil {
  376. writeErr = fmt.Errorf("%s: failed to write string table entry %d: %w", GeneratorPackageName, i, err)
  377. panic(writeErr)
  378. }
  379. }
  380. refs[i] = fileStringRef{
  381. off: off,
  382. length: len(payload),
  383. }
  384. }
  385. }
  386. return &FileStringTableReader{
  387. f: f,
  388. refs: refs,
  389. }
  390. }
  391. // At returns the string from the internal file using the reference's offset and length.
  392. func (fstr *FileStringTableReader) At(index int) string {
  393. if fstr == nil || fstr.f == nil {
  394. panic(fmt.Errorf("%s: failed to read file string table data", GeneratorPackageName))
  395. }
  396. if index < 0 || index >= len(fstr.refs) {
  397. panic(fmt.Errorf("%s: string table index out of bounds: %d", GeneratorPackageName, index))
  398. }
  399. ref := fstr.refs[index]
  400. if ref.length == 0 {
  401. return ""
  402. }
  403. b := make([]byte, ref.length)
  404. _, err := fstr.f.ReadAt(b, ref.off)
  405. if err != nil {
  406. return ""
  407. }
  408. // cast the allocated bytes to a string in-place, as we
  409. // were the ones that allocated the bytes
  410. return unsafe.String(unsafe.SliceData(b), len(b))
  411. }
  412. // Len returns the total number of strings loaded in the string table.
  413. func (fstr *FileStringTableReader) Len() int {
  414. if fstr == nil {
  415. return 0
  416. }
  417. return len(fstr.refs)
  418. }
  419. // Close for the file string table reader closes the file and deletes it.
  420. func (fstr *FileStringTableReader) Close() error {
  421. if fstr == nil || fstr.f == nil {
  422. return nil
  423. }
  424. path := fstr.f.Name()
  425. err := fstr.f.Close()
  426. fstr.f = nil
  427. fstr.refs = nil
  428. if path != "" {
  429. _ = os.Remove(path)
  430. }
  431. return err
  432. }
  433. //--------------------------------------------------------------------------
  434. // Codec Context
  435. //--------------------------------------------------------------------------
  436. // EncodingContext is a context object passed to the encoders to ensure reuse of buffer
  437. // and table data
  438. type EncodingContext struct {
  439. Buffer *util.Buffer
  440. Table *StringTableWriter
  441. }
  442. // IsStringTable returns true if the table is available
  443. func (ec *EncodingContext) IsStringTable() bool {
  444. return ec.Table != nil
  445. }
  446. // DecodingContext is a context object passed to the decoders to ensure parent objects
  447. // reuse as much data as possible
  448. type DecodingContext struct {
  449. Buffer *util.Buffer
  450. Table StringTableReader
  451. }
  452. // NewDecodingContextFromBytes creates a new DecodingContext instance using an byte slice
  453. func NewDecodingContextFromBytes(data []byte) *DecodingContext {
  454. var table StringTableReader
  455. buff := util.NewBufferFromBytes(data)
  456. // string table header validation
  457. if isBinaryTag(data, BinaryTagStringTable) {
  458. buff.ReadBytes(len(BinaryTagStringTable)) // strip tag length
  459. // always use a slice string table with a byte array since the
  460. // data is already in memory
  461. table = NewSliceStringTableReaderFrom(buff)
  462. }
  463. return &DecodingContext{
  464. Buffer: buff,
  465. Table: table,
  466. }
  467. }
  468. // NewDecodingContextFromReader creates a new DecodingContext instance using an io.Reader
  469. // implementation
  470. func NewDecodingContextFromReader(reader io.Reader) *DecodingContext {
  471. var table StringTableReader
  472. buff := util.NewBufferFromReader(reader)
  473. if isReaderBinaryTag(buff, BinaryTagStringTable) {
  474. buff.ReadBytes(len(BinaryTagStringTable)) // strip tag length
  475. // create correct string table implementation
  476. if IsBingenFileBackedStringTableEnabled() {
  477. table = NewFileStringTableReaderFrom(buff, BingenFileBackedStringTableDir())
  478. } else {
  479. table = NewSliceStringTableReaderFrom(buff)
  480. }
  481. }
  482. return &DecodingContext{
  483. Buffer: buff,
  484. Table: table,
  485. }
  486. }
  487. // IsStringTable returns true if the table is available
  488. func (dc *DecodingContext) IsStringTable() bool {
  489. return dc.Table != nil && dc.Table.Len() > 0
  490. }
  491. // Close will ensure that any string table resources and buffer resources are
  492. // cleaned up.
  493. func (dc *DecodingContext) Close() {
  494. if dc.Table != nil {
  495. _ = dc.Table.Close()
  496. dc.Table = nil
  497. }
  498. }
  499. //--------------------------------------------------------------------------
  500. // Binary Codec
  501. //--------------------------------------------------------------------------
  502. // BinEncoder is an encoding interface which defines a context based marshal contract.
  503. type BinEncoder interface {
  504. MarshalBinaryWithContext(*EncodingContext) error
  505. }
  506. // BinDecoder is a decoding interface which defines a context based unmarshal contract.
  507. type BinDecoder interface {
  508. UnmarshalBinaryWithContext(*DecodingContext) error
  509. }
  510. //--------------------------------------------------------------------------
  511. // NodeKey
  512. //--------------------------------------------------------------------------
  513. // MarshalBinary serializes the internal properties of this NodeKey instance
  514. // into a byte array
  515. func (target *NodeKey) MarshalBinary() (data []byte, err error) {
  516. ctx := &EncodingContext{
  517. Buffer: util.NewBuffer(),
  518. Table: nil,
  519. }
  520. e := target.MarshalBinaryWithContext(ctx)
  521. if e != nil {
  522. return nil, e
  523. }
  524. encBytes := ctx.Buffer.Bytes()
  525. return encBytes, nil
  526. }
  527. // MarshalBinaryWithContext serializes the internal properties of this NodeKey instance
  528. // into a byte array leveraging a predefined context.
  529. func (target *NodeKey) MarshalBinaryWithContext(ctx *EncodingContext) (err error) {
  530. // panics are recovered and propagated as errors
  531. defer func() {
  532. if r := recover(); r != nil {
  533. if e, ok := r.(error); ok {
  534. err = e
  535. } else if s, ok := r.(string); ok {
  536. err = fmt.Errorf("Unexpected panic: %s", s)
  537. } else {
  538. err = fmt.Errorf("Unexpected panic: %+v", r)
  539. }
  540. }
  541. }()
  542. buff := ctx.Buffer
  543. buff.WriteUInt8(DefaultCodecVersion) // version
  544. // --- [begin][write][alias](shared.Provider) ---
  545. if ctx.IsStringTable() {
  546. a := ctx.Table.AddOrGet(string(target.Provider))
  547. buff.WriteInt(a) // write table index
  548. } else {
  549. buff.WriteString(string(target.Provider)) // write string
  550. }
  551. // --- [end][write][alias](shared.Provider) ---
  552. // --- [begin][write][alias](NodePricingType) ---
  553. if ctx.IsStringTable() {
  554. b := ctx.Table.AddOrGet(string(target.PricingType))
  555. buff.WriteInt(b) // write table index
  556. } else {
  557. buff.WriteString(string(target.PricingType)) // write string
  558. }
  559. // --- [end][write][alias](NodePricingType) ---
  560. // --- [begin][write][alias](shared.UsageType) ---
  561. if ctx.IsStringTable() {
  562. c := ctx.Table.AddOrGet(string(target.UsageType))
  563. buff.WriteInt(c) // write table index
  564. } else {
  565. buff.WriteString(string(target.UsageType)) // write string
  566. }
  567. // --- [end][write][alias](shared.UsageType) ---
  568. if ctx.IsStringTable() {
  569. d := ctx.Table.AddOrGet(target.Region)
  570. buff.WriteInt(d) // write table index
  571. } else {
  572. buff.WriteString(target.Region) // write string
  573. }
  574. if ctx.IsStringTable() {
  575. e := ctx.Table.AddOrGet(target.NodeType)
  576. buff.WriteInt(e) // write table index
  577. } else {
  578. buff.WriteString(target.NodeType) // write string
  579. }
  580. if ctx.IsStringTable() {
  581. f := ctx.Table.AddOrGet(target.Family)
  582. buff.WriteInt(f) // write table index
  583. } else {
  584. buff.WriteString(target.Family) // write string
  585. }
  586. if ctx.IsStringTable() {
  587. g := ctx.Table.AddOrGet(target.DeviceType)
  588. buff.WriteInt(g) // write table index
  589. } else {
  590. buff.WriteString(target.DeviceType) // write string
  591. }
  592. return nil
  593. }
  594. // UnmarshalBinary uses the data passed byte array to set all the internal properties of
  595. // the NodeKey type
  596. func (target *NodeKey) UnmarshalBinary(data []byte) error {
  597. ctx := NewDecodingContextFromBytes(data)
  598. defer ctx.Close()
  599. err := target.UnmarshalBinaryWithContext(ctx)
  600. if err != nil {
  601. return err
  602. }
  603. return nil
  604. }
  605. // UnmarshalBinaryFromReader uses the io.Reader data to set all the internal properties of
  606. // the NodeKey type
  607. func (target *NodeKey) UnmarshalBinaryFromReader(reader io.Reader) error {
  608. ctx := NewDecodingContextFromReader(reader)
  609. defer ctx.Close()
  610. err := target.UnmarshalBinaryWithContext(ctx)
  611. if err != nil {
  612. return err
  613. }
  614. return nil
  615. }
  616. // UnmarshalBinaryWithContext uses the context containing a string table and binary buffer to set all the internal properties of
  617. // the NodeKey type
  618. func (target *NodeKey) UnmarshalBinaryWithContext(ctx *DecodingContext) (err error) {
  619. // panics are recovered and propagated as errors
  620. defer func() {
  621. if r := recover(); r != nil {
  622. if e, ok := r.(error); ok {
  623. err = e
  624. } else if s, ok := r.(string); ok {
  625. err = fmt.Errorf("Unexpected panic: %s", s)
  626. } else {
  627. err = fmt.Errorf("Unexpected panic: %+v", r)
  628. }
  629. }
  630. }()
  631. buff := ctx.Buffer
  632. version := buff.ReadUInt8()
  633. if version > DefaultCodecVersion {
  634. return fmt.Errorf("Invalid Version Unmarshaling NodeKey. Expected %d or less, got %d", DefaultCodecVersion, version)
  635. }
  636. // --- [begin][read][alias](shared.Provider) ---
  637. var a string
  638. if ctx.IsStringTable() {
  639. b := buff.ReadInt() // read string index
  640. a = ctx.Table.At(b)
  641. } else {
  642. a = buff.ReadString() // read string
  643. }
  644. target.Provider = shared.Provider(a)
  645. // --- [end][read][alias](shared.Provider) ---
  646. // --- [begin][read][alias](NodePricingType) ---
  647. var d string
  648. var f string
  649. if ctx.IsStringTable() {
  650. g := buff.ReadInt() // read string index
  651. f = ctx.Table.At(g)
  652. } else {
  653. f = buff.ReadString() // read string
  654. }
  655. e := f
  656. d = e
  657. target.PricingType = NodePricingType(d)
  658. // --- [end][read][alias](NodePricingType) ---
  659. // --- [begin][read][alias](shared.UsageType) ---
  660. var h string
  661. if ctx.IsStringTable() {
  662. l := buff.ReadInt() // read string index
  663. h = ctx.Table.At(l)
  664. } else {
  665. h = buff.ReadString() // read string
  666. }
  667. target.UsageType = shared.UsageType(h)
  668. // --- [end][read][alias](shared.UsageType) ---
  669. var o string
  670. if ctx.IsStringTable() {
  671. p := buff.ReadInt() // read string index
  672. o = ctx.Table.At(p)
  673. } else {
  674. o = buff.ReadString() // read string
  675. }
  676. n := o
  677. target.Region = n
  678. var r string
  679. if ctx.IsStringTable() {
  680. s := buff.ReadInt() // read string index
  681. r = ctx.Table.At(s)
  682. } else {
  683. r = buff.ReadString() // read string
  684. }
  685. q := r
  686. target.NodeType = q
  687. var u string
  688. if ctx.IsStringTable() {
  689. w := buff.ReadInt() // read string index
  690. u = ctx.Table.At(w)
  691. } else {
  692. u = buff.ReadString() // read string
  693. }
  694. t := u
  695. target.Family = t
  696. var y string
  697. if ctx.IsStringTable() {
  698. aa := buff.ReadInt() // read string index
  699. y = ctx.Table.At(aa)
  700. } else {
  701. y = buff.ReadString() // read string
  702. }
  703. x := y
  704. target.DeviceType = x
  705. return nil
  706. }
  707. //--------------------------------------------------------------------------
  708. // NodePricing
  709. //--------------------------------------------------------------------------
  710. // MarshalBinary serializes the internal properties of this NodePricing instance
  711. // into a byte array
  712. func (target *NodePricing) MarshalBinary() (data []byte, err error) {
  713. ctx := &EncodingContext{
  714. Buffer: util.NewBuffer(),
  715. Table: nil,
  716. }
  717. e := target.MarshalBinaryWithContext(ctx)
  718. if e != nil {
  719. return nil, e
  720. }
  721. encBytes := ctx.Buffer.Bytes()
  722. return encBytes, nil
  723. }
  724. // MarshalBinaryWithContext serializes the internal properties of this NodePricing instance
  725. // into a byte array leveraging a predefined context.
  726. func (target *NodePricing) MarshalBinaryWithContext(ctx *EncodingContext) (err error) {
  727. // panics are recovered and propagated as errors
  728. defer func() {
  729. if r := recover(); r != nil {
  730. if e, ok := r.(error); ok {
  731. err = e
  732. } else if s, ok := r.(string); ok {
  733. err = fmt.Errorf("Unexpected panic: %s", s)
  734. } else {
  735. err = fmt.Errorf("Unexpected panic: %+v", r)
  736. }
  737. }
  738. }()
  739. buff := ctx.Buffer
  740. buff.WriteUInt8(DefaultCodecVersion) // version
  741. buff.WriteFloat64(target.HourlyRate) // write float64
  742. return nil
  743. }
  744. // UnmarshalBinary uses the data passed byte array to set all the internal properties of
  745. // the NodePricing type
  746. func (target *NodePricing) UnmarshalBinary(data []byte) error {
  747. ctx := NewDecodingContextFromBytes(data)
  748. defer ctx.Close()
  749. err := target.UnmarshalBinaryWithContext(ctx)
  750. if err != nil {
  751. return err
  752. }
  753. return nil
  754. }
  755. // UnmarshalBinaryFromReader uses the io.Reader data to set all the internal properties of
  756. // the NodePricing type
  757. func (target *NodePricing) UnmarshalBinaryFromReader(reader io.Reader) error {
  758. ctx := NewDecodingContextFromReader(reader)
  759. defer ctx.Close()
  760. err := target.UnmarshalBinaryWithContext(ctx)
  761. if err != nil {
  762. return err
  763. }
  764. return nil
  765. }
  766. // UnmarshalBinaryWithContext uses the context containing a string table and binary buffer to set all the internal properties of
  767. // the NodePricing type
  768. func (target *NodePricing) UnmarshalBinaryWithContext(ctx *DecodingContext) (err error) {
  769. // panics are recovered and propagated as errors
  770. defer func() {
  771. if r := recover(); r != nil {
  772. if e, ok := r.(error); ok {
  773. err = e
  774. } else if s, ok := r.(string); ok {
  775. err = fmt.Errorf("Unexpected panic: %s", s)
  776. } else {
  777. err = fmt.Errorf("Unexpected panic: %+v", r)
  778. }
  779. }
  780. }()
  781. buff := ctx.Buffer
  782. version := buff.ReadUInt8()
  783. if version > DefaultCodecVersion {
  784. return fmt.Errorf("Invalid Version Unmarshaling NodePricing. Expected %d or less, got %d", DefaultCodecVersion, version)
  785. }
  786. a := buff.ReadFloat64() // read float64
  787. target.HourlyRate = a
  788. return nil
  789. }
  790. //--------------------------------------------------------------------------
  791. // PricingModelSet
  792. //--------------------------------------------------------------------------
  793. // MarshalBinary serializes the internal properties of this PricingModelSet instance
  794. // into a byte array
  795. func (target *PricingModelSet) MarshalBinary() (data []byte, err error) {
  796. ctx := &EncodingContext{
  797. Buffer: util.NewBuffer(),
  798. Table: nil,
  799. }
  800. e := target.MarshalBinaryWithContext(ctx)
  801. if e != nil {
  802. return nil, e
  803. }
  804. encBytes := ctx.Buffer.Bytes()
  805. return encBytes, nil
  806. }
  807. // MarshalBinaryWithContext serializes the internal properties of this PricingModelSet instance
  808. // into a byte array leveraging a predefined context.
  809. func (target *PricingModelSet) MarshalBinaryWithContext(ctx *EncodingContext) (err error) {
  810. // panics are recovered and propagated as errors
  811. defer func() {
  812. if r := recover(); r != nil {
  813. if e, ok := r.(error); ok {
  814. err = e
  815. } else if s, ok := r.(string); ok {
  816. err = fmt.Errorf("Unexpected panic: %s", s)
  817. } else {
  818. err = fmt.Errorf("Unexpected panic: %+v", r)
  819. }
  820. }
  821. }()
  822. buff := ctx.Buffer
  823. buff.WriteUInt8(DefaultCodecVersion) // version
  824. // --- [begin][write][reference](time.Time) ---
  825. a, errA := target.TimeStamp.MarshalBinary()
  826. if errA != nil {
  827. return errA
  828. }
  829. buff.WriteInt(len(a))
  830. buff.WriteBytes(a)
  831. // --- [end][write][reference](time.Time) ---
  832. // --- [begin][write][alias](PricingSourceType) ---
  833. if ctx.IsStringTable() {
  834. b := ctx.Table.AddOrGet(string(target.SourceType))
  835. buff.WriteInt(b) // write table index
  836. } else {
  837. buff.WriteString(string(target.SourceType)) // write string
  838. }
  839. // --- [end][write][alias](PricingSourceType) ---
  840. if ctx.IsStringTable() {
  841. c := ctx.Table.AddOrGet(target.SourceKey)
  842. buff.WriteInt(c) // write table index
  843. } else {
  844. buff.WriteString(target.SourceKey) // write string
  845. }
  846. if target.NodePricing == nil {
  847. buff.WriteUInt8(uint8(0)) // write nil byte
  848. } else {
  849. buff.WriteUInt8(uint8(1)) // write non-nil byte
  850. // --- [begin][write][map](map[NodeKey]NodePricing) ---
  851. buff.WriteInt(len(target.NodePricing)) // map length
  852. for v, z := range target.NodePricing {
  853. // --- [begin][write][struct](NodeKey) ---
  854. buff.WriteInt(0) // [compatibility, unused]
  855. errB := v.MarshalBinaryWithContext(ctx)
  856. if errB != nil {
  857. return errB
  858. }
  859. // --- [end][write][struct](NodeKey) ---
  860. // --- [begin][write][struct](NodePricing) ---
  861. buff.WriteInt(0) // [compatibility, unused]
  862. errC := z.MarshalBinaryWithContext(ctx)
  863. if errC != nil {
  864. return errC
  865. }
  866. // --- [end][write][struct](NodePricing) ---
  867. }
  868. // --- [end][write][map](map[NodeKey]NodePricing) ---
  869. }
  870. return nil
  871. }
  872. // UnmarshalBinary uses the data passed byte array to set all the internal properties of
  873. // the PricingModelSet type
  874. func (target *PricingModelSet) UnmarshalBinary(data []byte) error {
  875. ctx := NewDecodingContextFromBytes(data)
  876. defer ctx.Close()
  877. err := target.UnmarshalBinaryWithContext(ctx)
  878. if err != nil {
  879. return err
  880. }
  881. return nil
  882. }
  883. // UnmarshalBinaryFromReader uses the io.Reader data to set all the internal properties of
  884. // the PricingModelSet type
  885. func (target *PricingModelSet) UnmarshalBinaryFromReader(reader io.Reader) error {
  886. ctx := NewDecodingContextFromReader(reader)
  887. defer ctx.Close()
  888. err := target.UnmarshalBinaryWithContext(ctx)
  889. if err != nil {
  890. return err
  891. }
  892. return nil
  893. }
  894. // UnmarshalBinaryWithContext uses the context containing a string table and binary buffer to set all the internal properties of
  895. // the PricingModelSet type
  896. func (target *PricingModelSet) UnmarshalBinaryWithContext(ctx *DecodingContext) (err error) {
  897. // panics are recovered and propagated as errors
  898. defer func() {
  899. if r := recover(); r != nil {
  900. if e, ok := r.(error); ok {
  901. err = e
  902. } else if s, ok := r.(string); ok {
  903. err = fmt.Errorf("Unexpected panic: %s", s)
  904. } else {
  905. err = fmt.Errorf("Unexpected panic: %+v", r)
  906. }
  907. }
  908. }()
  909. buff := ctx.Buffer
  910. version := buff.ReadUInt8()
  911. if version > DefaultCodecVersion {
  912. return fmt.Errorf("Invalid Version Unmarshaling PricingModelSet. Expected %d or less, got %d", DefaultCodecVersion, version)
  913. }
  914. // --- [begin][read][reference](time.Time) ---
  915. a := &time.Time{}
  916. b := buff.ReadInt() // byte array length
  917. c := buff.ReadBytes(b) // byte array
  918. errA := a.UnmarshalBinary(c)
  919. if errA != nil {
  920. return errA
  921. }
  922. target.TimeStamp = *a
  923. // --- [end][read][reference](time.Time) ---
  924. // --- [begin][read][alias](PricingSourceType) ---
  925. var d string
  926. var f string
  927. if ctx.IsStringTable() {
  928. g := buff.ReadInt() // read string index
  929. f = ctx.Table.At(g)
  930. } else {
  931. f = buff.ReadString() // read string
  932. }
  933. e := f
  934. d = e
  935. target.SourceType = PricingSourceType(d)
  936. // --- [end][read][alias](PricingSourceType) ---
  937. var l string
  938. if ctx.IsStringTable() {
  939. m := buff.ReadInt() // read string index
  940. l = ctx.Table.At(m)
  941. } else {
  942. l = buff.ReadString() // read string
  943. }
  944. h := l
  945. target.SourceKey = h
  946. if buff.ReadUInt8() == uint8(0) {
  947. target.NodePricing = nil
  948. } else {
  949. // --- [begin][read][map](map[NodeKey]NodePricing) ---
  950. o := buff.ReadInt() // map len
  951. n := make(map[NodeKey]NodePricing, o)
  952. for i := 0; i < o; i++ {
  953. // --- [begin][read][struct](NodeKey) ---
  954. p := &NodeKey{}
  955. buff.ReadInt() // [compatibility, unused]
  956. errB := p.UnmarshalBinaryWithContext(ctx)
  957. if errB != nil {
  958. return errB
  959. }
  960. v := *p
  961. // --- [end][read][struct](NodeKey) ---
  962. // --- [begin][read][struct](NodePricing) ---
  963. q := &NodePricing{}
  964. buff.ReadInt() // [compatibility, unused]
  965. errC := q.UnmarshalBinaryWithContext(ctx)
  966. if errC != nil {
  967. return errC
  968. }
  969. z := *q
  970. // --- [end][read][struct](NodePricing) ---
  971. n[v] = z
  972. }
  973. target.NodePricing = n
  974. // --- [end][read][map](map[NodeKey]NodePricing) ---
  975. }
  976. return nil
  977. }