[#633] object: Prevent duplicate attributes by key

Keys of object attributes must be unique according to NeoFS specification.

Make `FormatValidator.Validate` method to return an error if at least one
attribute is duplicated.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-23 16:30:14 +03:00 committed by Alex Vanin
parent 6efeee5ce0
commit 01dd17e30a
2 changed files with 48 additions and 0 deletions

View file

@ -77,6 +77,10 @@ func (v *FormatValidator) Validate(obj *Object) error {
}
for ; obj != nil; obj = obj.GetParent() {
if err := v.checkAttributes(obj); err != nil {
return fmt.Errorf("invalid attributes: %w", err)
}
if err := v.validateSignatureKey(obj); err != nil {
return fmt.Errorf("(%T) could not validate signature key: %w", v, err)
}
@ -227,6 +231,26 @@ func expirationEpochAttribute(obj *Object) (uint64, error) {
return 0, errNoExpirationEpoch
}
var errDuplAttr = errors.New("duplication of attributes detected")
func (v *FormatValidator) checkAttributes(obj *Object) error {
as := obj.Attributes()
mUnique := make(map[string]struct{}, len(as))
for _, a := range as {
key := a.Key()
if _, was := mUnique[key]; was {
return errDuplAttr
}
mUnique[key] = struct{}{}
}
return nil
}
// WithNetState returns options to set network state interface.
//
// FIXME: network state is a required parameter.