Debian Codegraph @

attr.go

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