frostfs-node/pkg/innerring/processors/netmap/nodevalidation/locode/calls.go
Alexander Chuprov 9b113c3156
Some checks failed
DCO action / DCO (pull_request) Successful in 59s
Vulncheck / Vulncheck (pull_request) Successful in 1m4s
Pre-commit hooks / Pre-commit (pull_request) Successful in 1m55s
Build / Build Components (pull_request) Successful in 2m4s
Tests and linters / Staticcheck (pull_request) Successful in 2m38s
Tests and linters / Lint (pull_request) Successful in 3m16s
Tests and linters / Run gofumpt (pull_request) Successful in 3m54s
Tests and linters / Tests (pull_request) Successful in 4m12s
Tests and linters / gopls check (pull_request) Successful in 4m31s
Tests and linters / Tests with -race (pull_request) Successful in 4m38s
OCI image / Build container images (push) Failing after 18s
Vulncheck / Vulncheck (push) Successful in 1m2s
Pre-commit hooks / Pre-commit (push) Successful in 1m39s
Build / Build Components (push) Successful in 1m45s
Tests and linters / Staticcheck (push) Successful in 2m18s
Tests and linters / Run gofumpt (push) Successful in 2m46s
Tests and linters / Lint (push) Successful in 3m5s
Tests and linters / Tests with -race (push) Successful in 3m23s
Tests and linters / Tests (push) Successful in 3m52s
Tests and linters / gopls check (push) Successful in 4m18s
[#1613] morph: Add tracing for morph queries to neo-go
Signed-off-by: Alexander Chuprov <a.chuprov@yadro.com>
2025-02-05 16:38:20 +03:00

93 lines
2.2 KiB
Go

package locode
import (
"context"
"errors"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-locode-db/pkg/locode"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
)
var errMissingRequiredAttr = errors.New("missing required attribute in DB record")
// VerifyAndUpdate validates UN-LOCODE attribute of n
// and adds a group of related attributes.
//
// If n contains at least one of the LOCODE-derived attributes,
// an error is returned.
//
// If n contains UN-LOCODE attribute and its value does not
// match the UN/LOCODE format, an error is returned.
//
// New attributes are formed from the record of DB instance (Prm).
// If DB entry R was found w/o errors, new attributes are:
// - CountryCode: R.CountryCode().String();
// - Country: R.CountryName();
// - Location: Record.LocationName();
// - SubDivCode: R.SubDivCode();
// - SubDiv: R.SubDivName();
// - Continent: R.Continent().String().
//
// UN-LOCODE attribute remains untouched.
func (v *Validator) VerifyAndUpdate(_ context.Context, n *netmap.NodeInfo) error {
attrLocode := n.LOCODE()
if attrLocode == "" {
return nil
}
lc, err := locode.FromString(attrLocode)
if err != nil {
return fmt.Errorf("invalid locode value: %w", err)
}
record, err := v.db.Get(lc)
if err != nil {
return fmt.Errorf("could not get locode record from DB: %w", err)
}
countryCode := record.CountryCode()
if countryCode == nil {
return errMissingRequiredAttr
}
strCountryCode := countryCode.String()
if strCountryCode == "" {
return errMissingRequiredAttr
}
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
}