forked from TrueCloudLab/frostfs-node
[#1223] lens/tui: Add metabase schema
Signed-off-by: Aleksey Savchuk <a.savchuk@yadro.com>
This commit is contained in:
parent
b9043433a0
commit
1ae86f35a8
18 changed files with 1381 additions and 1 deletions
43
cmd/frostfs-lens/internal/schema/common/format.go
Normal file
43
cmd/frostfs-lens/internal/schema/common/format.go
Normal file
|
@ -0,0 +1,43 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
)
|
||||
|
||||
type FormatOptions struct {
|
||||
Color tcell.Color
|
||||
|
||||
Bold,
|
||||
Italic,
|
||||
Underline,
|
||||
StrikeThrough bool
|
||||
}
|
||||
|
||||
func Format(s string, opts FormatOptions) string {
|
||||
var boldTag, italicTag, underlineTag, strikeThroughTag string
|
||||
|
||||
switch {
|
||||
case opts.Bold:
|
||||
boldTag = "b"
|
||||
case opts.Italic:
|
||||
italicTag = "i"
|
||||
case opts.Underline:
|
||||
underlineTag = "u"
|
||||
case opts.StrikeThrough:
|
||||
strikeThroughTag = "s"
|
||||
}
|
||||
|
||||
attrs := fmt.Sprintf(
|
||||
"%s%s%s%s", boldTag, italicTag, underlineTag, strikeThroughTag,
|
||||
)
|
||||
color := strconv.FormatInt(int64(opts.Color.Hex()), 16)
|
||||
|
||||
return fmt.Sprintf("[#%06s::%s]%s[-::-]", color, attrs, s)
|
||||
}
|
||||
|
||||
func FormatSimple(s string, c tcell.Color) string {
|
||||
return Format(s, FormatOptions{Color: c})
|
||||
}
|
29
cmd/frostfs-lens/internal/schema/common/raw.go
Normal file
29
cmd/frostfs-lens/internal/schema/common/raw.go
Normal file
|
@ -0,0 +1,29 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"github.com/davecgh/go-spew/spew"
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/mr-tron/base58"
|
||||
)
|
||||
|
||||
type RawEntry struct {
|
||||
key, value []byte
|
||||
}
|
||||
|
||||
var RawParser Parser = rawParser
|
||||
|
||||
func rawParser(key, value []byte) (SchemaEntry, Parser, error) {
|
||||
return &RawEntry{key: key, value: value}, rawParser, nil
|
||||
}
|
||||
|
||||
func (r *RawEntry) String() string {
|
||||
return FormatSimple(base58.Encode(r.key), tcell.ColorRed)
|
||||
}
|
||||
|
||||
func (r *RawEntry) DetailedString() string {
|
||||
return spew.Sdump(r)
|
||||
}
|
||||
|
||||
func (r *RawEntry) Filter(string, any) FilterResult {
|
||||
return No
|
||||
}
|
81
cmd/frostfs-lens/internal/schema/common/schema.go
Normal file
81
cmd/frostfs-lens/internal/schema/common/schema.go
Normal file
|
@ -0,0 +1,81 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
panic(fmt.Errorf(
|
||||
"couldn't use that parser as a fallback parser, it returned an error: %w", err,
|
||||
))
|
||||
}
|
||||
return entry, next
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue