-
Notifications
You must be signed in to change notification settings - Fork 0
/
related.go
64 lines (57 loc) · 1.71 KB
/
related.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package action
import (
"github.com/pkg/errors"
"admini.dev/admini/app/lib/schema"
"admini.dev/admini/app/lib/schema/model"
"admini.dev/admini/app/util"
)
func GetSource(act *Action, schemata schema.Schemata) (*model.Package, error) {
k, err := act.Config.GetString(TypeSource.Key, false)
if err != nil {
return nil, errors.Wrap(err, "config key [source] must be provided")
}
sch, err := schemata.GetWithError(k)
if err != nil {
return nil, errors.Wrapf(err, "can't find source in project with key [%s]", k)
}
return sch.ModelsByPackage(), nil
}
func GetItem(act *Action, schemata schema.Schemata, key string) (any, []string, error) {
sch, err := GetSource(act, schemata)
if err != nil {
return nil, nil, err
}
p, err := act.Config.GetString(key, false)
if err != nil {
return nil, nil, errors.Wrap(err, "config key [package] must be provided")
}
i, remaining := sch.Get(util.StringSplitAndTrim(p, "/"))
if i == nil {
return nil, nil, errors.Errorf("no item found at path [%s]", p)
}
return i, remaining, nil
}
func GetPackage(act *Action, schemata schema.Schemata) (*model.Package, []string, error) {
i, remaining, err := GetItem(act, schemata, TypePackage.Key)
if err != nil {
return nil, nil, err
}
switch t := i.(type) {
case *model.Package:
return t, remaining, nil
default:
return nil, nil, errors.Errorf("unhandled type for package: %T", t)
}
}
func GetModel(act *Action, schemata schema.Schemata) (*model.Model, []string, error) {
i, remaining, err := GetItem(act, schemata, TypeModel.Key)
if err != nil {
return nil, nil, err
}
switch t := i.(type) {
case *model.Model:
return t, remaining, nil
default:
return nil, nil, errors.Errorf("unhandled type for model: %T", t)
}
}