All checks were successful
Vulncheck / Vulncheck (push) Successful in 1m9s
Pre-commit hooks / Pre-commit (push) Successful in 1m31s
Build / Build Components (push) Successful in 2m19s
Tests and linters / Tests (push) Successful in 3m20s
Tests and linters / gopls check (push) Successful in 3m40s
Tests and linters / Staticcheck (push) Successful in 3m42s
Tests and linters / Run gofumpt (push) Successful in 3m58s
Tests and linters / Lint (push) Successful in 4m26s
Tests and linters / Tests with -race (push) Successful in 5m53s
OCI image / Build container images (push) Successful in 4m4s
Change-Id: Id827da0cd9eef66efd806be6c9bc61044175a971 Signed-off-by: Ekaterina Lebedeva <ekaterina.lebedeva@yadro.com>
79 lines
1.7 KiB
Go
79 lines
1.7 KiB
Go
package common
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/assert"
|
|
)
|
|
|
|
type FilterResult byte
|
|
|
|
const (
|
|
No FilterResult = iota
|
|
Maybe
|
|
Yes
|
|
)
|
|
|
|
func IfThenElse(condition bool, onSuccess, onFailure FilterResult) FilterResult {
|
|
var res FilterResult
|
|
if condition {
|
|
res = onSuccess
|
|
} else {
|
|
res = onFailure
|
|
}
|
|
return res
|
|
}
|
|
|
|
type SchemaEntry interface {
|
|
String() string
|
|
DetailedString() string
|
|
Filter(typ string, val any) FilterResult
|
|
}
|
|
|
|
type (
|
|
Parser func(key, value []byte) (SchemaEntry, Parser, error)
|
|
FallbackParser func(key, value []byte) (SchemaEntry, Parser)
|
|
)
|
|
|
|
func Any(parsers ...Parser) Parser {
|
|
return func(key, value []byte) (SchemaEntry, Parser, error) {
|
|
var errs error
|
|
for _, parser := range parsers {
|
|
ret, next, err := parser(key, value)
|
|
if err == nil {
|
|
return ret, next, nil
|
|
}
|
|
errs = errors.Join(errs, err)
|
|
}
|
|
return nil, nil, fmt.Errorf("no parser succeeded: %w", errs)
|
|
}
|
|
}
|
|
|
|
func WithFallback(parser Parser, fallback FallbackParser) Parser {
|
|
if parser == nil {
|
|
return fallback.ToParser()
|
|
}
|
|
return func(key, value []byte) (SchemaEntry, Parser, error) {
|
|
entry, next, err := parser(key, value)
|
|
if err == nil {
|
|
return entry, WithFallback(next, fallback), nil
|
|
}
|
|
return fallback.ToParser()(key, value)
|
|
}
|
|
}
|
|
|
|
func (fp FallbackParser) ToParser() Parser {
|
|
return func(key, value []byte) (SchemaEntry, Parser, error) {
|
|
entry, next := fp(key, value)
|
|
return entry, next, nil
|
|
}
|
|
}
|
|
|
|
func (p Parser) ToFallbackParser() FallbackParser {
|
|
return func(key, value []byte) (SchemaEntry, Parser) {
|
|
entry, next, err := p(key, value)
|
|
assert.NoError(err, "couldn't use that parser as a fallback parser")
|
|
return entry, next
|
|
}
|
|
}
|