//////////////////////////////////////////////////////////////////////////////// // // 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 }