Merge pull request #3 from willglynn/add_enum_encoding

Add TagEnum encoding
This commit is contained in:
Fabian 2019-05-30 01:01:06 +02:00 committed by GitHub
commit c040c28fc2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 22 deletions

View File

@ -27,8 +27,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
switch value.(type) { switch value.(type) {
case int: case int:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
if err := e.encodeTag(tag); err != nil { if err := e.encodeTag(tag); err != nil {
@ -43,8 +43,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
return err return err
} }
case int16: case int16:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
if err := e.encodeTag(tag); err != nil { if err := e.encodeTag(tag); err != nil {
@ -59,8 +59,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
return err return err
} }
case int8: case int8:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
if err := e.encodeTag(tag); err != nil { if err := e.encodeTag(tag); err != nil {
@ -75,8 +75,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
return err return err
} }
case int32: case int32:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
if err := e.encodeTag(tag); err != nil { if err := e.encodeTag(tag); err != nil {
@ -91,8 +91,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
return err return err
} }
case int64: case int64:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
if err := e.encodeTag(tag); err != nil { if err := e.encodeTag(tag); err != nil {
@ -107,8 +107,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
return err return err
} }
case []int: case []int:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
for index, val := range value.([]int) { for index, val := range value.([]int) {
@ -131,8 +131,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
} }
} }
case []int16: case []int16:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
for index, val := range value.([]int16) { for index, val := range value.([]int16) {
@ -155,8 +155,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
} }
} }
case []int8: case []int8:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
for index, val := range value.([]int8) { for index, val := range value.([]int8) {
@ -179,8 +179,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
} }
} }
case []int32: case []int32:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
for index, val := range value.([]int32) { for index, val := range value.([]int32) {
@ -203,8 +203,8 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
} }
} }
case []int64: case []int64:
if tag != TagInteger { if tag != TagInteger && tag != TagEnum {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
for index, val := range value.([]int64) { for index, val := range value.([]int64) {
@ -228,7 +228,7 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
} }
case bool: case bool:
if tag != TagBoolean { if tag != TagBoolean {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
if err := e.encodeTag(tag); err != nil { if err := e.encodeTag(tag); err != nil {
@ -244,7 +244,7 @@ func (e *AttributeEncoder) Encode(attribute string, value interface{}) error {
} }
case []bool: case []bool:
if tag != TagBoolean { if tag != TagBoolean {
return fmt.Errorf("tag for attribte %s does not match with value type", attribute) return fmt.Errorf("tag for attribute %s does not match with value type", attribute)
} }
for index, val := range value.([]bool) { for index, val := range value.([]bool) {

View File

@ -35,6 +35,11 @@ var attributeTestCases = []struct {
Value: "utf-8", Value: "utf-8",
Bytes: []byte{71, 0, 18, 97, 116, 116, 114, 105, 98, 117, 116, 101, 115, 45, 99, 104, 97, 114, 115, 101, 116, 0, 5, 117, 116, 102, 45, 56}, Bytes: []byte{71, 0, 18, 97, 116, 116, 114, 105, 98, 117, 116, 101, 115, 45, 99, 104, 97, 114, 115, 101, 116, 0, 5, 117, 116, 102, 45, 56},
}, },
{
Attribute: "printer-state",
Value: 3,
Bytes: []byte("\x23\x00\x0dprinter-state\x00\x04\x00\x00\x00\x03"),
},
} }
func TestAttributeEncoding(t *testing.T) { func TestAttributeEncoding(t *testing.T) {