1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
|
/* Go IPP - IPP core protocol implementation in pure Go
*
* Copyright (C) 2020 and up by Alexander Pevzner (pzz@apevzner.com)
* See LICENSE for license terms and conditions
*
* Message attributes
*/
package goipp
import (
"fmt"
)
// Attributes represents a slice of attributes
type Attributes []Attribute
// Add Attribute to Attributes
func (attrs *Attributes) Add(attr Attribute) {
*attrs = append(*attrs, attr)
}
// Equal checks that attrs and attrs2 are equal
func (attrs Attributes) Equal(attrs2 Attributes) bool {
if len(attrs) != len(attrs2) {
return false
}
for i, attr := range attrs {
attr2 := attrs2[i]
if !attr.Equal(attr2) {
return false
}
}
return true
}
// Attribute represents a single attribute, which consist of
// the Name and one or more Values
type Attribute struct {
Name string // Attribute name
Values Values // Slice of values
}
// MakeAttribute makes Attribute with single value
func MakeAttribute(name string, tag Tag, value Value) Attribute {
attr := Attribute{Name: name}
attr.Values.Add(tag, value)
return attr
}
// Equal checks that Attribute is equal to another Attribute
// (i.e., names are the same and values are equal)
func (a Attribute) Equal(a2 Attribute) bool {
return a.Name == a2.Name && a.Values.Equal(a2.Values)
}
// Unpack attribute value from its wire representation
func (a *Attribute) unpack(tag Tag, value []byte) error {
var err error
var val Value
switch tag.Type() {
case TypeVoid, TypeCollection:
val = Void{}
case TypeInteger:
val = Integer(0)
case TypeBoolean:
val = Boolean(false)
case TypeString:
val = String("")
case TypeDateTime:
val = Time{}
case TypeResolution:
val = Resolution{}
case TypeRange:
val = Range{}
case TypeTextWithLang:
val = TextWithLang{}
case TypeBinary:
val = Binary(nil)
default:
panic(fmt.Sprintf("(Attribute) uppack(): tag=%s type=%s", tag, tag.Type()))
}
val, err = val.decode(value)
if err == nil {
a.Values.Add(tag, val)
} else {
err = fmt.Errorf("%s: %s", tag, err)
}
return err
}
|