-
Notifications
You must be signed in to change notification settings - Fork 8
/
definitions.go
131 lines (111 loc) · 3.22 KB
/
definitions.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package defs
import (
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/anypb"
"namespacelabs.dev/foundation/schema"
)
type Transformer func(*schema.SerializedInvocation)
type DefList struct {
descriptions []string
impls []proto.Message
transformers [][]Transformer
}
func (d *DefList) Add(description string, impl proto.Message, scope ...schema.PackageName) {
var sl schema.PackageList
sl.AddMultiple(scope...)
d.AddExt(description, impl, func(di *schema.SerializedInvocation) {
di.Scope = sl.PackageNamesAsString()
})
}
func (d *DefList) AddExt(description string, impl proto.Message, transformers ...Transformer) {
d.descriptions = append(d.descriptions, description)
d.impls = append(d.impls, impl)
d.transformers = append(d.transformers, transformers)
}
func (d *DefList) AddProgram(p DefList) {
d.descriptions = append(d.descriptions, p.descriptions...)
d.impls = append(d.impls, p.impls...)
d.transformers = append(d.transformers, p.transformers...)
}
func (d *DefList) Serialize() ([]*schema.SerializedInvocation, error) {
var defs []*schema.SerializedInvocation
for k := range d.descriptions {
serialized, err := anypb.New(d.impls[k])
if err != nil {
return nil, err
}
di := &schema.SerializedInvocation{
Description: d.descriptions[k],
Impl: serialized,
}
for _, transformer := range d.transformers[k] {
transformer(di)
}
defs = append(defs, di)
}
return defs, nil
}
func Category(names ...string) Transformer {
return func(di *schema.SerializedInvocation) {
if di.Order == nil {
di.Order = &schema.ScheduleOrder{
SchedCategory: names,
}
} else {
di.Order.SchedCategory = append(di.Order.SchedCategory, names...)
}
}
}
func DependsOn(names ...string) Transformer {
return func(di *schema.SerializedInvocation) {
if di.Order == nil {
di.Order = &schema.ScheduleOrder{
SchedAfterCategory: names,
}
} else {
di.Order.SchedAfterCategory = append(di.Order.SchedAfterCategory, names...)
}
}
}
func Consumes(name string) Transformer {
return func(di *schema.SerializedInvocation) {
di.RequiredOutput = append(di.RequiredOutput, name)
}
}
func MinimumVersion(version int32) Transformer {
return func(di *schema.SerializedInvocation) {
di.MinimumVersion = version
}
}
func Make[V MakeDefinition](ops ...V) ([]*schema.SerializedInvocation, error) {
var defs []*schema.SerializedInvocation
for _, m := range ops {
def, err := m.ToDefinition()
if err != nil {
return nil, err
}
defs = append(defs, def)
}
return defs, nil
}
func Static(description string, impl proto.Message) MakeDefinition {
return staticDef{description, impl}
}
type staticDef struct {
description string
impl proto.Message
}
func (def staticDef) ToDefinition(scope ...schema.PackageName) (*schema.SerializedInvocation, error) {
serialized, err := anypb.New(def.impl)
if err != nil {
return nil, err
}
return &schema.SerializedInvocation{
Description: def.description,
Impl: serialized,
Scope: schema.Strs(scope...),
}, nil
}