graph/encoding: provide attribute handling out of the box

This commit is contained in:
Dan Kortschak
2021-05-27 07:18:04 +09:30
parent c8613b2ab9
commit 97f387b38f
4 changed files with 175 additions and 19 deletions

View File

@@ -34,3 +34,36 @@ type Attributer interface {
type Attribute struct {
Key, Value string
}
// Attributes is a helper type providing simple attribute handling.
type Attributes []Attribute
// Attributes returns all of the receiver's attributes.
func (a *Attributes) Attributes() []Attribute {
return *a
}
// SetAttribute sets attr in the receiver. Calling SetAttribute with an
// Attribute with a Key that is in the collection replaces the existing
// value and calling with an empty Value removes the attribute from the
// collection if it exists. SetAttribute always returns nil.
func (a *Attributes) SetAttribute(attr Attribute) error {
if attr.Key == "" {
return nil
}
for i, v := range *a {
if v.Key == attr.Key {
if attr.Value == "" {
(*a)[i] = (*a)[len(*a)-1]
*a = (*a)[:len(*a)-1]
return nil
}
(*a)[i].Value = attr.Value
return nil
}
}
if attr.Value != "" {
*a = append(*a, attr)
}
return nil
}