shared_codecs.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // DO NOT MODIFY
  4. //
  5. // ┻━┻ ︵ヽ(`Д´)ノ︵ ┻━┻
  6. //
  7. //
  8. // This source file was automatically generated by bingen.
  9. //
  10. ////////////////////////////////////////////////////////////////////////////////
  11. package shared
  12. import (
  13. "fmt"
  14. util "github.com/opencost/opencost/core/pkg/util"
  15. "reflect"
  16. "strings"
  17. "sync"
  18. )
  19. const (
  20. // GeneratorPackageName is the package the generator is targetting
  21. GeneratorPackageName string = "shared"
  22. )
  23. // BinaryTags represent the formatting tag used for specific optimization features
  24. const (
  25. // BinaryTagStringTable is written and/or read prior to the existence of a string
  26. // table (where each index is encoded as a string entry in the resource
  27. BinaryTagStringTable string = "BGST"
  28. )
  29. const (
  30. // DefaultCodecVersion is used for any resources listed in the Default version set
  31. DefaultCodecVersion uint8 = 1
  32. )
  33. //--------------------------------------------------------------------------
  34. // Type Map
  35. //--------------------------------------------------------------------------
  36. // Generated type map for resolving interface implementations to
  37. // to concrete types
  38. var typeMap map[string]reflect.Type = map[string]reflect.Type{}
  39. //--------------------------------------------------------------------------
  40. // Type Helpers
  41. //--------------------------------------------------------------------------
  42. // isBinaryTag returns true when the first bytes in the provided binary matches the tag
  43. func isBinaryTag(data []byte, tag string) bool {
  44. return string(data[:len(tag)]) == tag
  45. }
  46. // appendBytes combines a and b into a new byte array
  47. func appendBytes(a []byte, b []byte) []byte {
  48. al := len(a)
  49. bl := len(b)
  50. tl := al + bl
  51. // allocate a new byte array for the combined
  52. // use native copy for speedy byte copying
  53. result := make([]byte, tl, tl)
  54. copy(result, a)
  55. copy(result[al:], b)
  56. return result
  57. }
  58. // typeToString determines the basic properties of the type, the qualifier, package path, and
  59. // type name, and returns the qualified type
  60. func typeToString(f interface{}) string {
  61. qual := ""
  62. t := reflect.TypeOf(f)
  63. if t.Kind() == reflect.Ptr {
  64. t = t.Elem()
  65. qual = "*"
  66. }
  67. return fmt.Sprintf("%s%s.%s", qual, t.PkgPath(), t.Name())
  68. }
  69. // resolveType uses the name of a type and returns the package, base type name, and whether
  70. // or not it's a pointer.
  71. func resolveType(t string) (pkg string, name string, isPtr bool) {
  72. isPtr = t[:1] == "*"
  73. if isPtr {
  74. t = t[1:]
  75. }
  76. slashIndex := strings.LastIndex(t, "/")
  77. if slashIndex >= 0 {
  78. t = t[slashIndex+1:]
  79. }
  80. parts := strings.Split(t, ".")
  81. if parts[0] == GeneratorPackageName {
  82. parts[0] = ""
  83. }
  84. pkg = parts[0]
  85. name = parts[1]
  86. return
  87. }
  88. //--------------------------------------------------------------------------
  89. // StringTable
  90. //--------------------------------------------------------------------------
  91. // StringTable maps strings to specific indices for encoding
  92. type StringTable struct {
  93. l *sync.Mutex
  94. indices map[string]int
  95. next int
  96. }
  97. // NewStringTable Creates a new StringTable instance with provided contents
  98. func NewStringTable(contents ...string) *StringTable {
  99. st := &StringTable{
  100. l: new(sync.Mutex),
  101. indices: make(map[string]int),
  102. next: len(contents),
  103. }
  104. for i, entry := range contents {
  105. st.indices[entry] = i
  106. }
  107. return st
  108. }
  109. // AddOrGet atomically retrieves a string entry's index if it exist. Otherwise, it will
  110. // add the entry and return the index.
  111. func (st *StringTable) AddOrGet(s string) int {
  112. st.l.Lock()
  113. defer st.l.Unlock()
  114. if ind, ok := st.indices[s]; ok {
  115. return ind
  116. }
  117. current := st.next
  118. st.next++
  119. st.indices[s] = current
  120. return current
  121. }
  122. // ToSlice Converts the contents to a string array for encoding.
  123. func (st *StringTable) ToSlice() []string {
  124. st.l.Lock()
  125. defer st.l.Unlock()
  126. if st.next == 0 {
  127. return []string{}
  128. }
  129. sl := make([]string, st.next, st.next)
  130. for s, i := range st.indices {
  131. sl[i] = s
  132. }
  133. return sl
  134. }
  135. // ToBytes Converts the contents to a binary encoded representation
  136. func (st *StringTable) ToBytes() []byte {
  137. buff := util.NewBuffer()
  138. buff.WriteBytes([]byte(BinaryTagStringTable)) // bingen table header
  139. strs := st.ToSlice()
  140. buff.WriteInt(len(strs)) // table length
  141. for _, s := range strs {
  142. buff.WriteString(s)
  143. }
  144. return buff.Bytes()
  145. }
  146. //--------------------------------------------------------------------------
  147. // Codec Context
  148. //--------------------------------------------------------------------------
  149. // EncodingContext is a context object passed to the encoders to ensure reuse of buffer
  150. // and table data
  151. type EncodingContext struct {
  152. Buffer *util.Buffer
  153. Table *StringTable
  154. }
  155. // IsStringTable returns true if the table is available
  156. func (ec *EncodingContext) IsStringTable() bool {
  157. return ec.Table != nil
  158. }
  159. // DecodingContext is a context object passed to the decoders to ensure parent objects
  160. // reuse as much data as possible
  161. type DecodingContext struct {
  162. Buffer *util.Buffer
  163. Table []string
  164. }
  165. // IsStringTable returns true if the table is available
  166. func (dc *DecodingContext) IsStringTable() bool {
  167. return len(dc.Table) > 0
  168. }
  169. //--------------------------------------------------------------------------
  170. // Binary Codec
  171. //--------------------------------------------------------------------------
  172. // BinEncoder is an encoding interface which defines a context based marshal contract.
  173. type BinEncoder interface {
  174. MarshalBinaryWithContext(*EncodingContext) error
  175. }
  176. // BinDecoder is a decoding interface which defines a context based unmarshal contract.
  177. type BinDecoder interface {
  178. UnmarshalBinaryWithContext(*DecodingContext) error
  179. }