Encode Attribute value
(tag Tag, v Value)
| 153 | |
| 154 | // Encode Attribute value |
| 155 | func (me *messageEncoder) encodeValue(tag Tag, v Value) error { |
| 156 | // Check Value type vs the Tag |
| 157 | tagType := tag.Type() |
| 158 | if tagType == TypeVoid { |
| 159 | v = Void{} // Ignore supplied value |
| 160 | } else if tagType != v.Type() { |
| 161 | return fmt.Errorf("Tag %s: %s value required, %s present", |
| 162 | tag, tagType, v.Type()) |
| 163 | } |
| 164 | |
| 165 | // Convert Value to bytes in wire representation. |
| 166 | data, err := v.encode() |
| 167 | if err != nil { |
| 168 | return err |
| 169 | } |
| 170 | |
| 171 | if len(data) > math.MaxInt16 { |
| 172 | return fmt.Errorf("Attribute value exceeds %d bytes", |
| 173 | math.MaxInt16) |
| 174 | } |
| 175 | |
| 176 | // TagExtension encoding rules enforcement. |
| 177 | if tag == TagExtension { |
| 178 | if len(data) < 4 { |
| 179 | return fmt.Errorf( |
| 180 | "Extension tag truncated (%d bytes)", len(data)) |
| 181 | } |
| 182 | |
| 183 | t := binary.BigEndian.Uint32(data) |
| 184 | if t > 0x7fffffff { |
| 185 | return fmt.Errorf( |
| 186 | "Extension tag 0x%8.8x out of range", t) |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Encode the value |
| 191 | err = me.encodeU16(uint16(len(data))) |
| 192 | if err == nil { |
| 193 | err = me.write(data) |
| 194 | } |
| 195 | |
| 196 | // Handle collection |
| 197 | if collection, ok := v.(Collection); ok { |
| 198 | return me.encodeCollection(tag, collection) |
| 199 | } |
| 200 | |
| 201 | return err |
| 202 | } |
| 203 | |
| 204 | // Encode collection |
| 205 | func (me *messageEncoder) encodeCollection(tag Tag, collection Collection) error { |
no test coverage detected