Skip to content

Commit

Permalink
[Go] Add helpers for otf PrimitiveValue
Browse files Browse the repository at this point in the history
  • Loading branch information
ethanf committed Sep 18, 2024
1 parent 46918b8 commit 9d33207
Show file tree
Hide file tree
Showing 4 changed files with 181 additions and 0 deletions.
17 changes: 17 additions & 0 deletions sbe-tool/src/main/golang/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
164 changes: 164 additions & 0 deletions sbe-tool/src/main/golang/otf/encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,158 @@ func NewPrimitiveValue(t PrimitiveType, value []byte) PrimitiveValue {
return p
}

func ParsePrimitiveValue(t PrimitiveType, value []byte) (PrimitiveValue, error) {
p := PrimitiveValue{PrimitiveType: t}

var err error

switch t {
case CHAR:
if len(value) > 1 {
p.arrayValue = string(value)
p.size = len(value)
} else {
p.asInt = int64(value[0])
p.size = 1
}
case INT8:
p.asInt, err = strconv.ParseInt(string(value), 10, 8)
p.size = 1
case INT16:
p.asInt, err = strconv.ParseInt(string(value), 10, 16)
p.size = 2
case INT32:
p.asInt, err = strconv.ParseInt(string(value), 10, 32)
p.size = 4
case INT64:
p.asInt, err = strconv.ParseInt(string(value), 10, 64)
p.size = 8
case UINT8:
p.asUInt, err = strconv.ParseUint(string(value), 10, 8)
p.size = 1
case UINT16:
p.asUInt, err = strconv.ParseUint(string(value), 10, 16)
p.size = 2
case UINT32:
p.asUInt, err = strconv.ParseUint(string(value), 10, 32)
p.size = 4
case UINT64:
p.asUInt, err = strconv.ParseUint(string(value), 10, 64)
p.size = 8
case FLOAT:
p.asDouble, err = strconv.ParseFloat(string(value), 32)
p.size = 4
case DOUBLE:
p.asDouble, err = strconv.ParseFloat(string(value), 64)
p.size = 8
default:
p.PrimitiveType = NONE
p.size = 0
}

return p, err
}

func NewNoneValue() PrimitiveValue {
return PrimitiveValue{}
}

func NewCharValue(value byte) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: CHAR,
size: 1,
asInt: int64(value),
}
}

func NewStringValue(value string) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: CHAR,
size: len(value),
arrayValue: value,
}
}

func NewInt8Value(value int8) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: INT8,
size: 1,
asInt: int64(value),
}
}

func NewInt16Value(value int16) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: INT16,
size: 2,
asInt: int64(value),
}
}

func NewInt32Value(value int32) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: INT32,
size: 4,
asInt: int64(value),
}
}

func NewInt64Value(value int64) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: INT64,
size: 8,
asInt: value,
}
}

func NewUInt8Value(value uint8) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: UINT8,
size: 1,
asUInt: uint64(value),
}
}

func NewUInt16Value(value uint16) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: UINT16,
size: 2,
asUInt: uint64(value),
}
}

func NewUInt32Value(value uint32) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: UINT32,
size: 4,
asUInt: uint64(value),
}
}

func NewUInt64Value(value uint64) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: UINT64,
size: 8,
asUInt: value,
}
}

func NewFloatValue(value float32) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: FLOAT,
size: 4,
asDouble: float64(value),
}
}

func NewDoubleValue(value float64) PrimitiveValue {
return PrimitiveValue{
PrimitiveType: DOUBLE,
size: 8,
asDouble: value,
}
}

func (p *PrimitiveValue) AsInt() int64 {
return p.asInt
}
Expand All @@ -276,6 +428,18 @@ func (p *PrimitiveValue) GetArray() string {
return p.arrayValue
}

func (p *PrimitiveValue) AsString() string {
if p.size <= 1 {
return string(byte(p.asInt))
}
for i := 0; i < len(p.arrayValue); i++ {
if p.arrayValue[i] == 0 {
return p.arrayValue[:i]
}
}
return p.arrayValue
}

func (p *PrimitiveValue) Size() int {
return p.size
}
Expand Down

0 comments on commit 9d33207

Please sign in to comment.