-
Notifications
You must be signed in to change notification settings - Fork 8
/
definitions.go
78 lines (65 loc) · 1.89 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
// 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 DefList struct {
descriptions []string
impls []proto.Message
scopes []schema.PackageList
}
func (d *DefList) Add(description string, impl proto.Message, scope ...schema.PackageName) {
d.descriptions = append(d.descriptions, description)
d.impls = append(d.impls, impl)
var sl schema.PackageList
sl.AddMultiple(scope...)
d.scopes = append(d.scopes, sl)
}
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
}
defs = append(defs, &schema.SerializedInvocation{
Description: d.descriptions[k],
Impl: serialized,
Scope: d.scopes[k].PackageNamesAsString(),
})
}
return defs, nil
}
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
}