2020-09-22 10:56:43 +00:00
|
|
|
package attributes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-11-10 07:08:33 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2020-09-22 10:56:43 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
pairSeparator = "/"
|
|
|
|
keyValueSeparator = ":"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errEmptyChain = errors.New("empty attribute chain")
|
|
|
|
errNonUniqueBucket = errors.New("attributes must contain unique keys")
|
|
|
|
errUnexpectedKey = errors.New("attributes contain unexpected key")
|
|
|
|
)
|
|
|
|
|
|
|
|
// ParseV2Attributes parses strings like "K1:V1/K2:V2/K3:V3" into netmap
|
2021-08-04 09:21:40 +00:00
|
|
|
// attributes. Supports escaped symbols "\:", "\/" and "\\".
|
2022-03-15 12:11:35 +00:00
|
|
|
func ParseV2Attributes(attrs []string, excl []string) ([]netmap.NodeAttribute, error) {
|
2020-09-22 10:56:43 +00:00
|
|
|
restricted := make(map[string]struct{}, len(excl))
|
|
|
|
for i := range excl {
|
|
|
|
restricted[excl[i]] = struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-11-16 10:26:35 +00:00
|
|
|
cache := make(map[string]*netmap.NodeAttribute, len(attrs))
|
2022-03-28 13:15:42 +00:00
|
|
|
result := make([]netmap.NodeAttribute, 0, len(attrs))
|
2020-09-22 10:56:43 +00:00
|
|
|
|
|
|
|
for i := range attrs {
|
|
|
|
line := strings.Trim(attrs[i], pairSeparator)
|
2021-08-04 09:21:40 +00:00
|
|
|
line = replaceEscaping(line, false) // replaced escaped symbols with non-printable symbols
|
2020-09-22 10:56:43 +00:00
|
|
|
chain := strings.Split(line, pairSeparator)
|
|
|
|
if len(chain) == 0 {
|
|
|
|
return nil, errEmptyChain
|
|
|
|
}
|
|
|
|
|
|
|
|
var parentKey string // backtrack parents in next pairs
|
|
|
|
|
|
|
|
for j := range chain {
|
|
|
|
pair := strings.Split(chain[j], keyValueSeparator)
|
|
|
|
if len(pair) != 2 {
|
|
|
|
return nil, fmt.Errorf("incorrect attribute pair %s", chain[j])
|
|
|
|
}
|
|
|
|
|
|
|
|
key := pair[0]
|
|
|
|
value := pair[1]
|
|
|
|
|
2021-08-31 15:34:55 +00:00
|
|
|
attribute, present := cache[key]
|
|
|
|
if present && attribute.Value() != value {
|
2020-09-22 10:56:43 +00:00
|
|
|
return nil, errNonUniqueBucket
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := restricted[key]; ok {
|
|
|
|
return nil, errUnexpectedKey
|
|
|
|
}
|
|
|
|
|
2021-08-31 15:34:55 +00:00
|
|
|
if !present {
|
2022-03-28 13:15:42 +00:00
|
|
|
result = append(result, netmap.NodeAttribute{})
|
|
|
|
attribute = &result[len(result)-1]
|
2021-09-01 09:55:09 +00:00
|
|
|
cache[key] = attribute
|
|
|
|
|
2021-08-31 15:34:55 +00:00
|
|
|
// replace non-printable symbols with escaped symbols without escape character
|
|
|
|
key = replaceEscaping(key, true)
|
|
|
|
value = replaceEscaping(value, true)
|
2021-08-04 09:21:40 +00:00
|
|
|
|
2021-08-31 15:34:55 +00:00
|
|
|
attribute.SetKey(key)
|
|
|
|
attribute.SetValue(value)
|
|
|
|
}
|
2020-09-22 10:56:43 +00:00
|
|
|
|
|
|
|
if parentKey != "" {
|
2021-08-31 15:34:55 +00:00
|
|
|
parentKeys := attribute.ParentKeys()
|
|
|
|
if !hasString(parentKeys, parentKey) {
|
|
|
|
attribute.SetParentKeys(append(parentKeys, parentKey)...)
|
|
|
|
}
|
2020-09-22 10:56:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parentKey = key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-28 13:15:42 +00:00
|
|
|
return result, nil
|
2020-09-22 10:56:43 +00:00
|
|
|
}
|
2021-08-04 09:21:40 +00:00
|
|
|
|
|
|
|
func replaceEscaping(target string, rollback bool) (s string) {
|
|
|
|
const escChar = `\`
|
|
|
|
|
|
|
|
var (
|
|
|
|
oldPairSep = escChar + pairSeparator
|
|
|
|
oldKVSep = escChar + keyValueSeparator
|
|
|
|
oldEsc = escChar + escChar
|
|
|
|
newPairSep = string(uint8(1))
|
|
|
|
newKVSep = string(uint8(2))
|
|
|
|
newEsc = string(uint8(3))
|
|
|
|
)
|
|
|
|
|
|
|
|
if rollback {
|
|
|
|
oldPairSep, oldKVSep, oldEsc = newPairSep, newKVSep, newEsc
|
|
|
|
newPairSep = pairSeparator
|
|
|
|
newKVSep = keyValueSeparator
|
|
|
|
newEsc = escChar
|
|
|
|
}
|
|
|
|
|
|
|
|
s = strings.ReplaceAll(target, oldEsc, newEsc)
|
|
|
|
s = strings.ReplaceAll(s, oldPairSep, newPairSep)
|
|
|
|
s = strings.ReplaceAll(s, oldKVSep, newKVSep)
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
2021-08-31 15:34:55 +00:00
|
|
|
|
|
|
|
func hasString(lst []string, v string) bool {
|
|
|
|
for i := range lst {
|
|
|
|
if lst[i] == v {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|