Skip to content

Commit

Permalink
idl: Fix parsing of hex integer values (#560)
Browse files Browse the repository at this point in the history
* idl: Fix parsing of hex integer values

* Address comments

* Fix literal space in Makefile
  • Loading branch information
KarboniteKream authored Feb 13, 2023
1 parent e6d71ae commit 69e460a
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 6 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,7 @@ COVER_IGNORE_FILES = \
$(wildcard plugin/api/*.go)

# literal space
space :=
space +=
space := $(subst ,, )

.PHONY: cover
cover:
Expand Down
30 changes: 30 additions & 0 deletions gen/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,36 @@ func TestConstants(t *testing.T) {
i interface{}
o interface{}
}{
{
"int16",
tk.Int16,
int16(12345),
},
{
"int32",
tk.Int32,
int32(1234567890),
},
{
"int64",
tk.Int64,
int64(1234567890123456789),
},
{
"hex16",
tk.Hex16,
int16(0x1234),
},
{
"hex32",
tk.Hex32,
int32(0x12345678),
},
{
"hex64",
tk.Hex64,
int64(0x1234567890abcdef),
},
{
"primitiveContainers",
tk.PrimitiveContainers,
Expand Down
31 changes: 31 additions & 0 deletions gen/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,37 @@ func TestEqualsEnumWithValues(t *testing.T) {
}
}

func TestValueOfEnumWithHexValues(t *testing.T) {
tests := []struct {
e te.EnumWithHexValues
i int32
}{
{te.EnumWithHexValuesX, 0x123},
{te.EnumWithHexValuesY, 0x456},
{te.EnumWithHexValuesZ, 0x789},
}
for _, tt := range tests {
assert.Equal(t, int32(tt.e), tt.i, "Value for %v does not match", tt.e)
}
}

func TestEqualsEnumWithHexValues(t *testing.T) {
tests := []struct {
lhs, rhs te.EnumWithHexValues
want bool
}{
{te.EnumWithHexValuesX, te.EnumWithHexValuesX, true},
{te.EnumWithHexValuesY, te.EnumWithHexValuesY, true},
{te.EnumWithHexValuesZ, te.EnumWithHexValuesZ, true},
{te.EnumWithHexValuesX, te.EnumWithHexValuesY, false},
{te.EnumWithHexValuesY, te.EnumWithHexValuesZ, false},
{te.EnumWithHexValuesZ, te.EnumWithHexValuesX, false},
}
for _, tt := range tests {
assert.Equal(t, tt.lhs.Equals(tt.rhs), tt.want)
}
}

func TestEnumDefaultWire(t *testing.T) {
tests := []struct {
e te.EnumDefault
Expand Down
16 changes: 14 additions & 2 deletions gen/internal/tests/constants/constants.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

216 changes: 214 additions & 2 deletions gen/internal/tests/enums/enums.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions gen/internal/tests/thrift/constants.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@ include "./structs.thrift"
include "./unions.thrift"
include "./typedefs.thrift"

const i16 int16 = 12345
const i32 int32 = 1234567890
const i64 int64 = 1234567890123456789

const i16 hex16 = 0x1234
const i32 hex32 = 0x12345678
const i64 hex64 = 0x1234567890abcdef

const containers.PrimitiveContainers primitiveContainers = {
"listOfInts": other_constants.listOfInts, // imported constant
"setOfStrings": ["foo", "bar"],
Expand Down
6 changes: 6 additions & 0 deletions gen/internal/tests/thrift/enums.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ enum EnumWithValues {
Z = 789,
}

enum EnumWithHexValues {
X = 0x123,
Y = 0x456,
Z = 0x789,
}

enum EnumWithDuplicateValues {
P, // 0
Q = -1,
Expand Down
5 changes: 5 additions & 0 deletions gen/quick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,11 @@ func TestQuickSuite(t *testing.T) {
Generator: enumValueGenerator(te.EnumWithValues_Values),
Kind: thriftEnum,
},
{
Sample: te.EnumWithHexValues(0),
Generator: enumValueGenerator(te.EnumWithHexValues_Values),
Kind: thriftEnum,
},
{
Sample: te.LowerCaseEnum(0),
Generator: enumValueGenerator(te.LowerCaseEnum_Values),
Expand Down
Loading

0 comments on commit 69e460a

Please sign in to comment.