| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770 |
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DO NOT MODIFY
- //
- // ┻━┻ ︵ヽ(`Д´)ノ︵ ┻━┻
- //
- //
- // This source file was automatically generated by bingen.
- //
- ////////////////////////////////////////////////////////////////////////////////
- package pricingmodel
- import (
- "fmt"
- "reflect"
- "strings"
- "sync"
- "time"
- "github.com/opencost/opencost/core/pkg/model/shared"
- "github.com/opencost/opencost/core/pkg/util"
- )
- const (
- // GeneratorPackageName is the package the generator is targetting
- GeneratorPackageName string = "pricingmodel"
- )
- // BinaryTags represent the formatting tag used for specific optimization features
- const (
- // BinaryTagStringTable is written and/or read prior to the existence of a string
- // table (where each index is encoded as a string entry in the resource
- BinaryTagStringTable string = "BGST"
- )
- const (
- // DefaultCodecVersion is used for any resources listed in the Default version set
- DefaultCodecVersion uint8 = 1
- )
- //--------------------------------------------------------------------------
- // Type Map
- //--------------------------------------------------------------------------
- // Generated type map for resolving interface implementations to
- // to concrete types
- var typeMap map[string]reflect.Type = map[string]reflect.Type{
- "NodeKey": reflect.TypeOf((*NodeKey)(nil)).Elem(),
- "NodePricing": reflect.TypeOf((*NodePricing)(nil)).Elem(),
- "PricingModelSet": reflect.TypeOf((*PricingModelSet)(nil)).Elem(),
- }
- //--------------------------------------------------------------------------
- // Type Helpers
- //--------------------------------------------------------------------------
- // isBinaryTag returns true when the first bytes in the provided binary matches the tag
- func isBinaryTag(data []byte, tag string) bool {
- return string(data[:len(tag)]) == tag
- }
- // appendBytes combines a and b into a new byte array
- func appendBytes(a []byte, b []byte) []byte {
- al := len(a)
- bl := len(b)
- tl := al + bl
- // allocate a new byte array for the combined
- // use native copy for speedy byte copying
- result := make([]byte, tl, tl)
- copy(result, a)
- copy(result[al:], b)
- return result
- }
- // typeToString determines the basic properties of the type, the qualifier, package path, and
- // type name, and returns the qualified type
- func typeToString(f interface{}) string {
- qual := ""
- t := reflect.TypeOf(f)
- if t.Kind() == reflect.Ptr {
- t = t.Elem()
- qual = "*"
- }
- return fmt.Sprintf("%s%s.%s", qual, t.PkgPath(), t.Name())
- }
- // resolveType uses the name of a type and returns the package, base type name, and whether
- // or not it's a pointer.
- func resolveType(t string) (pkg string, name string, isPtr bool) {
- isPtr = t[:1] == "*"
- if isPtr {
- t = t[1:]
- }
- slashIndex := strings.LastIndex(t, "/")
- if slashIndex >= 0 {
- t = t[slashIndex+1:]
- }
- parts := strings.Split(t, ".")
- if parts[0] == GeneratorPackageName {
- parts[0] = ""
- }
- pkg = parts[0]
- name = parts[1]
- return
- }
- //--------------------------------------------------------------------------
- // StringTable
- //--------------------------------------------------------------------------
- // StringTable maps strings to specific indices for encoding
- type StringTable struct {
- l *sync.Mutex
- indices map[string]int
- next int
- }
- // NewStringTable Creates a new StringTable instance with provided contents
- func NewStringTable(contents ...string) *StringTable {
- st := &StringTable{
- l: new(sync.Mutex),
- indices: make(map[string]int),
- next: len(contents),
- }
- for i, entry := range contents {
- st.indices[entry] = i
- }
- return st
- }
- // AddOrGet atomically retrieves a string entry's index if it exist. Otherwise, it will
- // add the entry and return the index.
- func (st *StringTable) AddOrGet(s string) int {
- st.l.Lock()
- defer st.l.Unlock()
- if ind, ok := st.indices[s]; ok {
- return ind
- }
- current := st.next
- st.next++
- st.indices[s] = current
- return current
- }
- // ToSlice Converts the contents to a string array for encoding.
- func (st *StringTable) ToSlice() []string {
- st.l.Lock()
- defer st.l.Unlock()
- if st.next == 0 {
- return []string{}
- }
- sl := make([]string, st.next, st.next)
- for s, i := range st.indices {
- sl[i] = s
- }
- return sl
- }
- // ToBytes Converts the contents to a binary encoded representation
- func (st *StringTable) ToBytes() []byte {
- buff := util.NewBuffer()
- buff.WriteBytes([]byte(BinaryTagStringTable)) // bingen table header
- strs := st.ToSlice()
- buff.WriteInt(len(strs)) // table length
- for _, s := range strs {
- buff.WriteString(s)
- }
- return buff.Bytes()
- }
- //--------------------------------------------------------------------------
- // Codec Context
- //--------------------------------------------------------------------------
- // EncodingContext is a context object passed to the encoders to ensure reuse of buffer
- // and table data
- type EncodingContext struct {
- Buffer *util.Buffer
- Table *StringTable
- }
- // IsStringTable returns true if the table is available
- func (ec *EncodingContext) IsStringTable() bool {
- return ec.Table != nil
- }
- // DecodingContext is a context object passed to the decoders to ensure parent objects
- // reuse as much data as possible
- type DecodingContext struct {
- Buffer *util.Buffer
- Table []string
- }
- // IsStringTable returns true if the table is available
- func (dc *DecodingContext) IsStringTable() bool {
- return len(dc.Table) > 0
- }
- //--------------------------------------------------------------------------
- // Binary Codec
- //--------------------------------------------------------------------------
- // BinEncoder is an encoding interface which defines a context based marshal contract.
- type BinEncoder interface {
- MarshalBinaryWithContext(*EncodingContext) error
- }
- // BinDecoder is a decoding interface which defines a context based unmarshal contract.
- type BinDecoder interface {
- UnmarshalBinaryWithContext(*DecodingContext) error
- }
- //--------------------------------------------------------------------------
- // NodeKey
- //--------------------------------------------------------------------------
- // MarshalBinary serializes the internal properties of this NodeKey instance
- // into a byte array
- func (target *NodeKey) MarshalBinary() (data []byte, err error) {
- ctx := &EncodingContext{
- Buffer: util.NewBuffer(),
- Table: nil,
- }
- e := target.MarshalBinaryWithContext(ctx)
- if e != nil {
- return nil, e
- }
- encBytes := ctx.Buffer.Bytes()
- return encBytes, nil
- }
- // MarshalBinaryWithContext serializes the internal properties of this NodeKey instance
- // into a byte array leveraging a predefined context.
- func (target *NodeKey) MarshalBinaryWithContext(ctx *EncodingContext) (err error) {
- // panics are recovered and propagated as errors
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else if s, ok := r.(string); ok {
- err = fmt.Errorf("Unexpected panic: %s", s)
- } else {
- err = fmt.Errorf("Unexpected panic: %+v", r)
- }
- }
- }()
- buff := ctx.Buffer
- buff.WriteUInt8(DefaultCodecVersion) // version
- // --- [begin][write][alias](shared.Provider) ---
- if ctx.IsStringTable() {
- a := ctx.Table.AddOrGet(string(target.Provider))
- buff.WriteInt(a) // write table index
- } else {
- buff.WriteString(string(target.Provider)) // write string
- }
- // --- [end][write][alias](shared.Provider) ---
- if ctx.IsStringTable() {
- b := ctx.Table.AddOrGet(target.Region)
- buff.WriteInt(b) // write table index
- } else {
- buff.WriteString(target.Region) // write string
- }
- if ctx.IsStringTable() {
- c := ctx.Table.AddOrGet(target.NodeType)
- buff.WriteInt(c) // write table index
- } else {
- buff.WriteString(target.NodeType) // write string
- }
- // --- [begin][write][alias](shared.UsageType) ---
- if ctx.IsStringTable() {
- d := ctx.Table.AddOrGet(string(target.UsageType))
- buff.WriteInt(d) // write table index
- } else {
- buff.WriteString(string(target.UsageType)) // write string
- }
- // --- [end][write][alias](shared.UsageType) ---
- if ctx.IsStringTable() {
- e := ctx.Table.AddOrGet(target.Family)
- buff.WriteInt(e) // write table index
- } else {
- buff.WriteString(target.Family) // write string
- }
- if ctx.IsStringTable() {
- f := ctx.Table.AddOrGet(target.DeviceType)
- buff.WriteInt(f) // write table index
- } else {
- buff.WriteString(target.DeviceType) // write string
- }
- // --- [begin][write][alias](NodePricingType) ---
- if ctx.IsStringTable() {
- g := ctx.Table.AddOrGet(string(target.PricingType))
- buff.WriteInt(g) // write table index
- } else {
- buff.WriteString(string(target.PricingType)) // write string
- }
- // --- [end][write][alias](NodePricingType) ---
- return nil
- }
- // UnmarshalBinary uses the data passed byte array to set all the internal properties of
- // the NodeKey type
- func (target *NodeKey) UnmarshalBinary(data []byte) error {
- var table []string
- buff := util.NewBufferFromBytes(data)
- // string table header validation
- if isBinaryTag(data, BinaryTagStringTable) {
- buff.ReadBytes(len(BinaryTagStringTable)) // strip tag length
- tl := buff.ReadInt() // table length
- if tl > 0 {
- table = make([]string, tl, tl)
- for i := 0; i < tl; i++ {
- table[i] = buff.ReadString()
- }
- }
- }
- ctx := &DecodingContext{
- Buffer: buff,
- Table: table,
- }
- err := target.UnmarshalBinaryWithContext(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- // UnmarshalBinaryWithContext uses the context containing a string table and binary buffer to set all the internal properties of
- // the NodeKey type
- func (target *NodeKey) UnmarshalBinaryWithContext(ctx *DecodingContext) (err error) {
- // panics are recovered and propagated as errors
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else if s, ok := r.(string); ok {
- err = fmt.Errorf("Unexpected panic: %s", s)
- } else {
- err = fmt.Errorf("Unexpected panic: %+v", r)
- }
- }
- }()
- buff := ctx.Buffer
- version := buff.ReadUInt8()
- if version > DefaultCodecVersion {
- return fmt.Errorf("Invalid Version Unmarshaling NodeKey. Expected %d or less, got %d", DefaultCodecVersion, version)
- }
- // --- [begin][read][alias](shared.Provider) ---
- var a string
- if ctx.IsStringTable() {
- b := buff.ReadInt() // read string index
- a = ctx.Table[b]
- } else {
- a = buff.ReadString() // read string
- }
- target.Provider = shared.Provider(a)
- // --- [end][read][alias](shared.Provider) ---
- var e string
- if ctx.IsStringTable() {
- f := buff.ReadInt() // read string index
- e = ctx.Table[f]
- } else {
- e = buff.ReadString() // read string
- }
- d := e
- target.Region = d
- var h string
- if ctx.IsStringTable() {
- k := buff.ReadInt() // read string index
- h = ctx.Table[k]
- } else {
- h = buff.ReadString() // read string
- }
- g := h
- target.NodeType = g
- // --- [begin][read][alias](shared.UsageType) ---
- var l string
- if ctx.IsStringTable() {
- m := buff.ReadInt() // read string index
- l = ctx.Table[m]
- } else {
- l = buff.ReadString() // read string
- }
- target.UsageType = shared.UsageType(l)
- // --- [end][read][alias](shared.UsageType) ---
- var p string
- if ctx.IsStringTable() {
- q := buff.ReadInt() // read string index
- p = ctx.Table[q]
- } else {
- p = buff.ReadString() // read string
- }
- o := p
- target.Family = o
- var s string
- if ctx.IsStringTable() {
- t := buff.ReadInt() // read string index
- s = ctx.Table[t]
- } else {
- s = buff.ReadString() // read string
- }
- r := s
- target.DeviceType = r
- // --- [begin][read][alias](NodePricingType) ---
- var u string
- var x string
- if ctx.IsStringTable() {
- y := buff.ReadInt() // read string index
- x = ctx.Table[y]
- } else {
- x = buff.ReadString() // read string
- }
- w := x
- u = w
- target.PricingType = NodePricingType(u)
- // --- [end][read][alias](NodePricingType) ---
- return nil
- }
- //--------------------------------------------------------------------------
- // NodePricing
- //--------------------------------------------------------------------------
- // MarshalBinary serializes the internal properties of this NodePricing instance
- // into a byte array
- func (target *NodePricing) MarshalBinary() (data []byte, err error) {
- ctx := &EncodingContext{
- Buffer: util.NewBuffer(),
- Table: nil,
- }
- e := target.MarshalBinaryWithContext(ctx)
- if e != nil {
- return nil, e
- }
- encBytes := ctx.Buffer.Bytes()
- return encBytes, nil
- }
- // MarshalBinaryWithContext serializes the internal properties of this NodePricing instance
- // into a byte array leveraging a predefined context.
- func (target *NodePricing) MarshalBinaryWithContext(ctx *EncodingContext) (err error) {
- // panics are recovered and propagated as errors
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else if s, ok := r.(string); ok {
- err = fmt.Errorf("Unexpected panic: %s", s)
- } else {
- err = fmt.Errorf("Unexpected panic: %+v", r)
- }
- }
- }()
- buff := ctx.Buffer
- buff.WriteUInt8(DefaultCodecVersion) // version
- buff.WriteFloat64(target.HourlyRate) // write float64
- return nil
- }
- // UnmarshalBinary uses the data passed byte array to set all the internal properties of
- // the NodePricing type
- func (target *NodePricing) UnmarshalBinary(data []byte) error {
- var table []string
- buff := util.NewBufferFromBytes(data)
- // string table header validation
- if isBinaryTag(data, BinaryTagStringTable) {
- buff.ReadBytes(len(BinaryTagStringTable)) // strip tag length
- tl := buff.ReadInt() // table length
- if tl > 0 {
- table = make([]string, tl, tl)
- for i := 0; i < tl; i++ {
- table[i] = buff.ReadString()
- }
- }
- }
- ctx := &DecodingContext{
- Buffer: buff,
- Table: table,
- }
- err := target.UnmarshalBinaryWithContext(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- // UnmarshalBinaryWithContext uses the context containing a string table and binary buffer to set all the internal properties of
- // the NodePricing type
- func (target *NodePricing) UnmarshalBinaryWithContext(ctx *DecodingContext) (err error) {
- // panics are recovered and propagated as errors
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else if s, ok := r.(string); ok {
- err = fmt.Errorf("Unexpected panic: %s", s)
- } else {
- err = fmt.Errorf("Unexpected panic: %+v", r)
- }
- }
- }()
- buff := ctx.Buffer
- version := buff.ReadUInt8()
- if version > DefaultCodecVersion {
- return fmt.Errorf("Invalid Version Unmarshaling NodePricing. Expected %d or less, got %d", DefaultCodecVersion, version)
- }
- a := buff.ReadFloat64() // read float64
- target.HourlyRate = a
- return nil
- }
- //--------------------------------------------------------------------------
- // PricingModelSet
- //--------------------------------------------------------------------------
- // MarshalBinary serializes the internal properties of this PricingModelSet instance
- // into a byte array
- func (target *PricingModelSet) MarshalBinary() (data []byte, err error) {
- ctx := &EncodingContext{
- Buffer: util.NewBuffer(),
- Table: nil,
- }
- e := target.MarshalBinaryWithContext(ctx)
- if e != nil {
- return nil, e
- }
- encBytes := ctx.Buffer.Bytes()
- return encBytes, nil
- }
- // MarshalBinaryWithContext serializes the internal properties of this PricingModelSet instance
- // into a byte array leveraging a predefined context.
- func (target *PricingModelSet) MarshalBinaryWithContext(ctx *EncodingContext) (err error) {
- // panics are recovered and propagated as errors
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else if s, ok := r.(string); ok {
- err = fmt.Errorf("Unexpected panic: %s", s)
- } else {
- err = fmt.Errorf("Unexpected panic: %+v", r)
- }
- }
- }()
- buff := ctx.Buffer
- buff.WriteUInt8(DefaultCodecVersion) // version
- // --- [begin][write][reference](time.Time) ---
- a, errA := target.TimeStamp.MarshalBinary()
- if errA != nil {
- return errA
- }
- buff.WriteInt(len(a))
- buff.WriteBytes(a)
- // --- [end][write][reference](time.Time) ---
- if ctx.IsStringTable() {
- b := ctx.Table.AddOrGet(string(target.SourceType))
- buff.WriteInt(b) // write table index
- } else {
- buff.WriteString(string(target.SourceType)) // write string
- }
- if target.NodePricing == nil {
- buff.WriteUInt8(uint8(0)) // write nil byte
- } else {
- buff.WriteUInt8(uint8(1)) // write non-nil byte
- // --- [begin][write][map](map[NodeKey]NodePricing) ---
- buff.WriteInt(len(target.NodePricing)) // map length
- for v, z := range target.NodePricing {
- // --- [begin][write][struct](NodeKey) ---
- buff.WriteInt(0) // [compatibility, unused]
- errB := v.MarshalBinaryWithContext(ctx)
- if errB != nil {
- return errB
- }
- // --- [end][write][struct](NodeKey) ---
- // --- [begin][write][struct](NodePricing) ---
- buff.WriteInt(0) // [compatibility, unused]
- errC := z.MarshalBinaryWithContext(ctx)
- if errC != nil {
- return errC
- }
- // --- [end][write][struct](NodePricing) ---
- }
- // --- [end][write][map](map[NodeKey]NodePricing) ---
- }
- if ctx.IsStringTable() {
- i := ctx.Table.AddOrGet(target.SourceKey)
- buff.WriteInt(i) // write table index
- } else {
- buff.WriteString(target.SourceKey) // write string
- }
- return nil
- }
- // UnmarshalBinary uses the data passed byte array to set all the internal properties of
- // the PricingModelSet type
- func (target *PricingModelSet) UnmarshalBinary(data []byte) error {
- var table []string
- buff := util.NewBufferFromBytes(data)
- // string table header validation
- if isBinaryTag(data, BinaryTagStringTable) {
- buff.ReadBytes(len(BinaryTagStringTable)) // strip tag length
- tl := buff.ReadInt() // table length
- if tl > 0 {
- table = make([]string, tl, tl)
- for i := 0; i < tl; i++ {
- table[i] = buff.ReadString()
- }
- }
- }
- ctx := &DecodingContext{
- Buffer: buff,
- Table: table,
- }
- err := target.UnmarshalBinaryWithContext(ctx)
- if err != nil {
- return err
- }
- return nil
- }
- // UnmarshalBinaryWithContext uses the context containing a string table and binary buffer to set all the internal properties of
- // the PricingModelSet type
- func (target *PricingModelSet) UnmarshalBinaryWithContext(ctx *DecodingContext) (err error) {
- // panics are recovered and propagated as errors
- defer func() {
- if r := recover(); r != nil {
- if e, ok := r.(error); ok {
- err = e
- } else if s, ok := r.(string); ok {
- err = fmt.Errorf("Unexpected panic: %s", s)
- } else {
- err = fmt.Errorf("Unexpected panic: %+v", r)
- }
- }
- }()
- buff := ctx.Buffer
- version := buff.ReadUInt8()
- if version > DefaultCodecVersion {
- return fmt.Errorf("Invalid Version Unmarshaling PricingModelSet. Expected %d or less, got %d", DefaultCodecVersion, version)
- }
- // --- [begin][read][reference](time.Time) ---
- a := &time.Time{}
- b := buff.ReadInt() // byte array length
- c := buff.ReadBytes(b) // byte array
- errA := a.UnmarshalBinary(c)
- if errA != nil {
- return errA
- }
- target.TimeStamp = *a
- // --- [end][read][reference](time.Time) ---
- var e string
- if ctx.IsStringTable() {
- f := buff.ReadInt() // read string index
- e = ctx.Table[f]
- } else {
- e = buff.ReadString() // read string
- }
- d := e
- target.SourceType = PricingSourceType(d)
- if buff.ReadUInt8() == uint8(0) {
- target.NodePricing = nil
- } else {
- // --- [begin][read][map](map[NodeKey]NodePricing) ---
- h := buff.ReadInt() // map len
- g := make(map[NodeKey]NodePricing, h)
- for i := 0; i < h; i++ {
- // --- [begin][read][struct](NodeKey) ---
- k := &NodeKey{}
- buff.ReadInt() // [compatibility, unused]
- errB := k.UnmarshalBinaryWithContext(ctx)
- if errB != nil {
- return errB
- }
- v := *k
- // --- [end][read][struct](NodeKey) ---
- // --- [begin][read][struct](NodePricing) ---
- l := &NodePricing{}
- buff.ReadInt() // [compatibility, unused]
- errC := l.UnmarshalBinaryWithContext(ctx)
- if errC != nil {
- return errC
- }
- z := *l
- // --- [end][read][struct](NodePricing) ---
- g[v] = z
- }
- target.NodePricing = g
- // --- [end][read][map](map[NodeKey]NodePricing) ---
- }
- var m string
- if ctx.IsStringTable() {
- n := buff.ReadInt() // read string index
- m = ctx.Table[n]
- } else {
- m = buff.ReadString() // read string
- }
- target.SourceKey = m
- return nil
- }
|