pricingmodel_codecs.go 35 KB

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