Skip to content

Commit

Permalink
Added enum support
Browse files Browse the repository at this point in the history
  • Loading branch information
POPSuL committed Aug 14, 2024
1 parent f7c776c commit 5637ca6
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
2 changes: 2 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
8 changes: 6 additions & 2 deletions decoder/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 8 additions & 0 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 11 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const (
TypeBoolean Type = 'b'
TypeArray Type = 'a'
TypeObject Type = 'o'
TypeEnum Type = 'e'
)

type Object struct {
Expand All @@ -34,6 +35,7 @@ type Value struct {
Str string
Array map[*Value]*Value
Object *Object
Enum string
}

func (v *Value) String() string {
Expand All @@ -60,6 +62,8 @@ func (v *Value) String() string {
return sb.String()
case TypeObject:
return fmt.Sprintf("<object of %s>", v.Object.Name)
case TypeEnum:
return v.Enum
}
return "<unknown>"
}
Expand All @@ -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,
Expand Down

0 comments on commit 5637ca6

Please sign in to comment.