Debian Codegraph @

plumbing/protocol/packp/capability/list.go

raw
1 package capability
2
3 import (
4 "bytes"
5 "errors"
6 "fmt"
7 "strings"
8 )
9
10 var (
11 // ErrArgumentsRequired is returned if no arguments are giving with a
12 // capability that requires arguments
13 ErrArgumentsRequired = errors.New("arguments required")
14 // ErrArguments is returned if arguments are given with a capabilities that
15 // not supports arguments
16 ErrArguments = errors.New("arguments not allowed")
17 // ErrEmptyArgument is returned when an empty value is given
18 ErrEmptyArgument = errors.New("empty argument")
19 // ErrMultipleArguments multiple argument given to a capabilities that not
20 // support it
21 ErrMultipleArguments = errors.New("multiple arguments not allowed")
22 )
23
24 // List represents a list of capabilities
25 type List struct {
26 m map[Capability]*entry
27 sort []string
28 }
29
30 type entry struct {
31 Name Capability
32 Values []string
33 }
34
35 // NewList returns a new List of capabilities
36 func NewList() *List {
37 return &List{
38 m: make(map[Capability]*entry),
39 }
40 }
41
42 // IsEmpty returns true if the List is empty
43 func (l *List) IsEmpty() bool {
44 return len(l.sort) == 0
45 }
46
47 // Decode decodes list of capabilities from raw into the list
48 func (l *List) Decode(raw []byte) error {
49 // git 1.x receive pack used to send a leading space on its
50 // git-receive-pack capabilities announcement. We just trim space to be
51 // tolerant to space changes in different versions.
52 raw = bytes.TrimSpace(raw)
53
54 if len(raw) == 0 {
55 return nil
56 }
57
58 for _, data := range bytes.Split(raw, []byte{' '}) {
59 pair := bytes.SplitN(data, []byte{'='}, 2)
60
61 c := Capability(pair[0])
62 if len(pair) == 1 {
63 if err := l.Add(c); err != nil {
64 return err
65 }
66
67 continue
68 }
69
70 if err := l.Add(c, string(pair[1])); err != nil {
71 return err
72 }
73 }
74
75 return nil
76 }
77
78 // Get returns the values for a capability
79 func (l *List) Get(capability Capability) []string {
80 if _, ok := l.m[capability]; !ok {
81 return nil
82 }
83
84 return l.m[capability].Values
85 }
86
87 // Set sets a capability removing the previous values
88 func (l *List) Set(capability Capability, values ...string) error {
89 if _, ok := l.m[capability]; ok {
90 l.m[capability].Values = l.m[capability].Values[:0]
91 }
92 return l.Add(capability, values...)
93 }
94
95 // Add adds a capability, values are optional
96 func (l *List) Add(c Capability, values ...string) error {
97 if err := l.validate(c, values); err != nil {
98 return err
99 }
100
101 if !l.Supports(c) {
102 l.m[c] = &entry{Name: c}
103 l.sort = append(l.sort, c.String())
104 }
105
106 if len(values) == 0 {
107 return nil
108 }
109
110 if known[c] && !multipleArgument[c] && len(l.m[c].Values) > 0 {
111 return ErrMultipleArguments
112 }
113
114 l.m[c].Values = append(l.m[c].Values, values...)
115 return nil
116 }
117
118 func (l *List) validateNoEmptyArgs(values []string) error {
119 for _, v := range values {
120 if v == "" {
121 return ErrEmptyArgument
122 }
123 }
124 return nil
125 }
126
127 func (l *List) validate(c Capability, values []string) error {
128 if !known[c] {
129 return l.validateNoEmptyArgs(values)
130 }
131 if requiresArgument[c] && len(values) == 0 {
132 return ErrArgumentsRequired
133 }
134
135 if !requiresArgument[c] && len(values) != 0 {
136 return ErrArguments
137 }
138
139 if !multipleArgument[c] && len(values) > 1 {
140 return ErrMultipleArguments
141 }
142 return l.validateNoEmptyArgs(values)
143 }
144
145 // Supports returns true if capability is present
146 func (l *List) Supports(capability Capability) bool {
147 _, ok := l.m[capability]
148 return ok
149 }
150
151 // Delete deletes a capability from the List
152 func (l *List) Delete(capability Capability) {
153 if !l.Supports(capability) {
154 return
155 }
156
157 delete(l.m, capability)
158 for i, c := range l.sort {
159 if c != string(capability) {
160 continue
161 }
162
163 l.sort = append(l.sort[:i], l.sort[i+1:]...)
164 return
165 }
166 }
167
168 // All returns a slice with all defined capabilities.
169 func (l *List) All() []Capability {
170 var cs []Capability
171 for _, key := range l.sort {
172 cs = append(cs, Capability(key))
173 }
174
175 return cs
176 }
177
178 // String generates the capabilities strings, the capabilities are sorted in
179 // insertion order
180 func (l *List) String() string {
181 var o []string
182 for _, key := range l.sort {
183 cap := l.m[Capability(key)]
184 if len(cap.Values) == 0 {
185 o = append(o, key)
186 continue
187 }
188
189 for _, value := range cap.Values {
190 o = append(o, fmt.Sprintf("%s=%s", key, value))
191 }
192 }
193
194 return strings.Join(o, " ")
195 }