Encode attribute
(attr Attribute, checkTag bool)
| 68 | |
| 69 | // Encode attribute |
| 70 | func (me *messageEncoder) encodeAttr(attr Attribute, checkTag bool) error { |
| 71 | // Wire format |
| 72 | // 1 byte: Tag |
| 73 | // 2 bytes: len(Name) |
| 74 | // variable: name |
| 75 | // 2 bytes: len(Value) |
| 76 | // variable Value |
| 77 | // |
| 78 | // And each additional value comes as attribute |
| 79 | // without name |
| 80 | if len(attr.Values) == 0 { |
| 81 | return errors.New("Attribute without value") |
| 82 | } |
| 83 | |
| 84 | name := attr.Name |
| 85 | for _, val := range attr.Values { |
| 86 | tag := val.T |
| 87 | |
| 88 | if checkTag { |
| 89 | if tag.IsDelimiter() || tag == TagMemberName || tag == TagEndCollection { |
| 90 | return fmt.Errorf("Tag %s cannot be used with value", tag) |
| 91 | } |
| 92 | |
| 93 | if uint(tag) >= 0x100 { |
| 94 | return fmt.Errorf("Tag %s out of range", tag) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | err := me.encodeTag(tag) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | |
| 103 | err = me.encodeName(name) |
| 104 | if err != nil { |
| 105 | return err |
| 106 | } |
| 107 | |
| 108 | err = me.encodeValue(val.T, val.V) |
| 109 | if err != nil { |
| 110 | return err |
| 111 | } |
| 112 | |
| 113 | name = "" // Each additional value comes without name |
| 114 | } |
| 115 | |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // Encode 8-bit integer |
| 120 | func (me *messageEncoder) encodeU8(v uint8) error { |
no test coverage detected