feat: unifood base with GET dishes (#1)
Features: - CLI structure with verbs and resources - Application config and parameters - Output formatters - Initial resource: dishes Reviewed-on: #1 Co-authored-by: bdoerfchen <git@bissendorf.co> Co-committed-by: bdoerfchen <git@bissendorf.co>
This commit is contained in:
29
core/interfaces/handler.go
Normal file
29
core/interfaces/handler.go
Normal file
@ -0,0 +1,29 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"git.bissendorf.co/bissendorf/unifood/m/v2/core/interfaces/params"
|
||||
)
|
||||
|
||||
type ResourceCommand[T any] struct {
|
||||
Name string
|
||||
Aliases []string
|
||||
Description string
|
||||
Verbs []Verb
|
||||
Handler ResourceHandler
|
||||
}
|
||||
|
||||
type ResourceHandler interface {
|
||||
GetParametersForVerb(verb Verb) []params.Registration
|
||||
}
|
||||
|
||||
type GetHandler interface {
|
||||
Get(ctx context.Context, params params.Container) (*ResourceList, error)
|
||||
}
|
||||
|
||||
type Verb string
|
||||
|
||||
const (
|
||||
VerbGet Verb = "get"
|
||||
)
|
||||
7
core/interfaces/http.go
Normal file
7
core/interfaces/http.go
Normal file
@ -0,0 +1,7 @@
|
||||
package interfaces
|
||||
|
||||
import "context"
|
||||
|
||||
type QueryClient[T any] interface {
|
||||
Get(ctx context.Context, queryStatement string, selectStatement string, allowCache bool) (*T, error)
|
||||
}
|
||||
14
core/interfaces/output.go
Normal file
14
core/interfaces/output.go
Normal file
@ -0,0 +1,14 @@
|
||||
package interfaces
|
||||
|
||||
import (
|
||||
"io"
|
||||
)
|
||||
|
||||
type Formatter interface {
|
||||
Format(object *ResourceList) (io.Reader, error)
|
||||
}
|
||||
|
||||
type TableOutput interface {
|
||||
ColumnNames() []string
|
||||
Columns() []any
|
||||
}
|
||||
69
core/interfaces/params/params.go
Normal file
69
core/interfaces/params/params.go
Normal file
@ -0,0 +1,69 @@
|
||||
package params
|
||||
|
||||
import "errors"
|
||||
|
||||
var (
|
||||
ErrParamNotFound = errors.New("parameter not found")
|
||||
)
|
||||
|
||||
type Registration struct {
|
||||
Name string
|
||||
ShortHand string
|
||||
Description string
|
||||
DefaultFunc func() string
|
||||
ParseFunc ParseFn
|
||||
}
|
||||
|
||||
type ParseFn func(value string) (any, error)
|
||||
|
||||
var ParseString ParseFn = func(value string) (any, error) { return value, nil }
|
||||
|
||||
type Container struct {
|
||||
params map[string]Value
|
||||
}
|
||||
|
||||
type Value struct {
|
||||
value *string
|
||||
parseFn ParseFn
|
||||
}
|
||||
|
||||
func (c *Container) Register(key string, parseFn ParseFn) *string {
|
||||
if c.params == nil {
|
||||
c.params = make(map[string]Value)
|
||||
}
|
||||
|
||||
item, exists := c.params[key]
|
||||
if exists {
|
||||
return item.value
|
||||
}
|
||||
|
||||
var str string
|
||||
value := Value{
|
||||
parseFn: parseFn,
|
||||
value: &str,
|
||||
}
|
||||
c.params[key] = value
|
||||
return value.value
|
||||
}
|
||||
|
||||
func (c *Container) GetValue(key string) (any, error) {
|
||||
item, exists := c.params[key]
|
||||
if !exists {
|
||||
return nil, ErrParamNotFound
|
||||
}
|
||||
|
||||
return item.parseFn(*item.value)
|
||||
}
|
||||
|
||||
// Returns a map of all parameters. Parameters that produce errors during parsing are ignored.
|
||||
func (c *Container) ToMap() (out map[string]any) {
|
||||
out = make(map[string]any)
|
||||
for key := range c.params {
|
||||
v, err := c.GetValue(key)
|
||||
if err == nil {
|
||||
out[key] = v
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
12
core/interfaces/resource.go
Normal file
12
core/interfaces/resource.go
Normal file
@ -0,0 +1,12 @@
|
||||
package interfaces
|
||||
|
||||
type Resource interface {
|
||||
Kind() string
|
||||
|
||||
Name() string
|
||||
}
|
||||
|
||||
type ResourceList struct {
|
||||
ItemKind string
|
||||
Items []Resource
|
||||
}
|
||||
Reference in New Issue
Block a user