[#1513] Upgrade NeoFS SDK Go with changed netmap package

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-06-09 02:18:26 +03:00 committed by LeL
parent 24b4c1ecf4
commit 21d2f8f861
70 changed files with 878 additions and 992 deletions

View file

@ -30,24 +30,12 @@ var errMissingRequiredAttr = errors.New("missing required attribute in DB record
//
// UN-LOCODE attribute remains untouched.
func (v *Validator) VerifyAndUpdate(n *netmap.NodeInfo) error {
mAttr := uniqueAttributes(n.Attributes())
// check if the derived attributes are presented
for attrKey := range v.mAttr {
if _, ok := mAttr[attrKey]; ok {
return fmt.Errorf("attribute derived from %s is presented: %s",
netmap.AttrUNLOCODE,
attrKey,
)
}
}
attrLocode, ok := mAttr[netmap.AttrUNLOCODE]
if !ok {
attrLocode := n.LOCODE()
if attrLocode == "" {
return nil
}
lc, err := locode.FromString(attrLocode.Value())
lc, err := locode.FromString(attrLocode)
if err != nil {
return fmt.Errorf("invalid locode value: %w", err)
}
@ -57,41 +45,48 @@ func (v *Validator) VerifyAndUpdate(n *netmap.NodeInfo) error {
return fmt.Errorf("could not get locode record from DB: %w", err)
}
for attrKey, attrDesc := range v.mAttr {
attrVal := attrDesc.converter(record)
if attrVal == "" {
if !attrDesc.optional {
return errMissingRequiredAttr
}
continue
}
var a netmap.NodeAttribute
a.SetKey(attrKey)
a.SetValue(attrVal)
mAttr[attrKey] = a
countryCode := record.CountryCode()
if countryCode == nil {
return errMissingRequiredAttr
}
as := n.Attributes()
as = as[:0]
for _, attr := range mAttr {
as = append(as, attr)
strCountryCode := countryCode.String()
if strCountryCode == "" {
return errMissingRequiredAttr
}
n.SetAttributes(as...)
countryName := record.CountryName()
if countryName == "" {
return errMissingRequiredAttr
}
locationName := record.LocationName()
if locationName == "" {
return errMissingRequiredAttr
}
continent := record.Continent()
if continent == nil {
return errMissingRequiredAttr
}
continentName := continent.String()
if continentName == "" {
return errMissingRequiredAttr
}
n.SetCountryCode(strCountryCode)
n.SetCountryName(countryName)
n.SetLocationName(locationName)
n.SetContinentName(continentName)
if subDivCode := record.SubDivCode(); subDivCode != "" {
n.SetSubdivisionCode(subDivCode)
}
if subDivName := record.SubDivName(); subDivName != "" {
n.SetSubdivisionName(subDivName)
}
return nil
}
func uniqueAttributes(as []netmap.NodeAttribute) map[string]netmap.NodeAttribute {
mAttr := make(map[string]netmap.NodeAttribute, len(as))
for _, attr := range as {
mAttr[attr.Key()] = attr
}
return mAttr
}

View file

@ -34,40 +34,21 @@ func (x db) Get(lc *locodestd.LOCODE) (locode.Record, error) {
return r, nil
}
func addAttrKV(n *netmap.NodeInfo, key, val string) {
var a netmap.NodeAttribute
a.SetKey(key)
a.SetValue(val)
n.SetAttributes(append(n.Attributes(), a)...)
}
func addLocodeAttrValue(n *netmap.NodeInfo, val string) {
addAttrKV(n, netmap.AttrUNLOCODE, val)
n.SetLOCODE(val)
}
func addLocodeAttr(n *netmap.NodeInfo, lc locodestd.LOCODE) {
addLocodeAttrValue(n, fmt.Sprintf("%s %s", lc[0], lc[1]))
n.SetLOCODE(fmt.Sprintf("%s %s", lc[0], lc[1]))
}
func nodeInfoWithSomeAttrs() *netmap.NodeInfo {
n := netmap.NewNodeInfo()
var n netmap.NodeInfo
addAttrKV(n, "key1", "val1")
addAttrKV(n, "key2", "val2")
n.SetAttribute("key1", "val1")
n.SetAttribute("key2", "val2")
return n
}
func containsAttr(n *netmap.NodeInfo, key, val string) bool {
for _, a := range n.Attributes() {
if a.Key() == key && a.Value() == val {
return true
}
}
return false
return &n
}
func TestValidator_VerifyAndUpdate(t *testing.T) {
@ -108,41 +89,11 @@ func TestValidator_VerifyAndUpdate(t *testing.T) {
validator := locode.New(p)
t.Run("w/ derived attributes", func(t *testing.T) {
fn := func(withLocode bool) {
for _, derivedAttr := range []string{
netmap.AttrCountryCode,
netmap.AttrCountry,
netmap.AttrLocation,
netmap.AttrSubDivCode,
netmap.AttrSubDiv,
netmap.AttrContinent,
} {
n := nodeInfoWithSomeAttrs()
addAttrKV(n, derivedAttr, "some value")
if withLocode {
addLocodeAttr(n, r.LOCODE)
}
err := validator.VerifyAndUpdate(n)
require.Error(t, err)
}
}
fn(true)
fn(false)
})
t.Run("w/o locode", func(t *testing.T) {
n := nodeInfoWithSomeAttrs()
attrs := n.Attributes()
err := validator.VerifyAndUpdate(n)
require.NoError(t, err)
require.Equal(t, attrs, n.Attributes())
})
t.Run("w/ locode", func(t *testing.T) {
@ -168,22 +119,14 @@ func TestValidator_VerifyAndUpdate(t *testing.T) {
addLocodeAttr(n, r.LOCODE)
attrs := n.Attributes()
err := validator.VerifyAndUpdate(n)
require.NoError(t, err)
outAttrs := n.Attributes()
for _, a := range attrs {
require.Contains(t, outAttrs, a)
}
require.True(t, containsAttr(n, netmap.AttrCountryCode, rec.CountryCode().String()))
require.True(t, containsAttr(n, netmap.AttrCountry, rec.CountryName()))
require.True(t, containsAttr(n, netmap.AttrLocation, rec.LocationName()))
require.True(t, containsAttr(n, netmap.AttrSubDivCode, rec.SubDivCode()))
require.True(t, containsAttr(n, netmap.AttrSubDiv, rec.SubDivName()))
require.True(t, containsAttr(n, netmap.AttrContinent, rec.Continent().String()))
require.Equal(t, rec.CountryCode().String(), n.Attribute("CountryCode"))
require.Equal(t, rec.CountryName(), n.Attribute("Country"))
require.Equal(t, rec.LocationName(), n.Attribute("Location"))
require.Equal(t, rec.SubDivCode(), n.Attribute("SubDivCode"))
require.Equal(t, rec.SubDivName(), n.Attribute("SubDiv"))
require.Equal(t, rec.Continent().String(), n.Attribute("Continent"))
})
}

View file

@ -1,30 +0,0 @@
package locode
type attrDescriptor struct {
optional bool
converter func(Record) string
}
func countryCodeValue(r Record) (val string) {
return r.CountryCode().String()
}
func countryValue(r Record) string {
return r.CountryName()
}
func locationValue(r Record) string {
return r.LocationName()
}
func subDivCodeValue(r Record) string {
return r.SubDivCode()
}
func subDivValue(r Record) string {
return r.SubDivName()
}
func continentValue(r Record) string {
return r.Continent().String()
}

View file

@ -1,9 +1,5 @@
package locode
import (
"github.com/nspcc-dev/neofs-sdk-go/netmap"
)
// Prm groups the required parameters of the Validator's constructor.
//
// All values must comply with the requirements imposed on them.
@ -26,8 +22,6 @@ type Prm struct {
// the Validator is immediately ready to work through API.
type Validator struct {
db DB
mAttr map[string]attrDescriptor
}
// New creates a new instance of the Validator.
@ -39,16 +33,5 @@ type Validator struct {
func New(prm Prm) *Validator {
return &Validator{
db: prm.DB,
mAttr: map[string]attrDescriptor{
netmap.AttrCountryCode: {converter: countryCodeValue},
netmap.AttrCountry: {converter: countryValue},
netmap.AttrLocation: {converter: locationValue},
netmap.AttrSubDivCode: {converter: subDivCodeValue, optional: true},
netmap.AttrSubDiv: {converter: subDivValue, optional: true},
netmap.AttrContinent: {converter: continentValue},
},
}
}