From 5637ca6961aeab04b9a2630634d516a637981d53 Mon Sep 17 00:00:00 2001 From: Viktor Suprun Date: Wed, 14 Aug 2024 21:02:04 +1100 Subject: [PATCH] Added enum support --- cmd/main.go | 2 ++ decoder/decoder.go | 8 ++++++-- decoder/decoder_test.go | 8 ++++++++ types/types.go | 11 +++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 68d79f8..1041639 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -70,6 +70,8 @@ func printZval(sb *strings.Builder, v *types.Value, level int) { } sb.Write(bytes.Repeat([]byte{' '}, (level-1)*2)) sb.WriteString("}") + case types.TypeEnum: + sb.WriteString(v.Enum) } } diff --git a/decoder/decoder.go b/decoder/decoder.go index d5388e2..9ab8101 100644 --- a/decoder/decoder.go +++ b/decoder/decoder.go @@ -97,13 +97,17 @@ func parseNext(in []byte, i int) (*types.Value, int, error) { Type: types.TypeFloat, Float: val, }, offset + 1, nil - case 's': + case 's', 'E': val, offset, err := peekInteger(in, i) if err != nil { return nil, i, err } i = offset + 2 - return types.NewString(string(in[i : i+val])), i + val + 2, nil + if b == 's' { + return types.NewString(string(in[i : i+val])), i + val + 2, nil + } else { + return types.NewEnum(string(in[i : i+val])), i + val + 2, nil + } case 'N': return &types.Value{ Type: types.TypeNull, diff --git a/decoder/decoder_test.go b/decoder/decoder_test.go index 4ec8181..f42ba58 100644 --- a/decoder/decoder_test.go +++ b/decoder/decoder_test.go @@ -33,6 +33,14 @@ func TestString(t *testing.T) { assert.Equal(t, "s", out.Str) } +func TestEnum(t *testing.T) { + in := "E:6:\"Test:A\";" + out, err := Decode([]byte(in)) + assert.NoError(t, err) + assert.Equal(t, TypeEnum, out.Type) + assert.Equal(t, "Test:A", out.Enum) +} + func TestNull(t *testing.T) { in := "N;" out, err := Decode([]byte(in)) diff --git a/types/types.go b/types/types.go index af6a445..452c087 100644 --- a/types/types.go +++ b/types/types.go @@ -16,6 +16,7 @@ const ( TypeBoolean Type = 'b' TypeArray Type = 'a' TypeObject Type = 'o' + TypeEnum Type = 'e' ) type Object struct { @@ -34,6 +35,7 @@ type Value struct { Str string Array map[*Value]*Value Object *Object + Enum string } func (v *Value) String() string { @@ -60,6 +62,8 @@ func (v *Value) String() string { return sb.String() case TypeObject: return fmt.Sprintf("", v.Object.Name) + case TypeEnum: + return v.Enum } return "" } @@ -71,6 +75,13 @@ func NewString(s string) *Value { } } +func NewEnum(s string) *Value { + return &Value{ + Type: TypeEnum, + Enum: s, + } +} + func NewObject(s string) *Object { return &Object{ Name: s,