-
Notifications
You must be signed in to change notification settings - Fork 8
/
resourceclass.go
52 lines (42 loc) · 1.45 KB
/
resourceclass.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
// 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 cuefrontendopaque
import (
"context"
"namespacelabs.dev/foundation/internal/fnerrors"
"namespacelabs.dev/foundation/internal/frontend/fncue"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/pkggraph"
)
type cueResourceClass struct {
Intent *cueResourceClassType `json:"intent"`
Produces *cueResourceClassType `json:"produces"`
}
type cueResourceClassType struct {
Type string `json:"type"`
Source string `json:"source"`
}
func parseResourceClass(ctx context.Context, loc pkggraph.Location, name string, v *fncue.CueV) (*schema.ResourceClass, error) {
var bits cueResourceClass
if err := v.Val.Decode(&bits); err != nil {
return nil, err
}
if bits.Intent == nil {
return nil, fnerrors.NewWithLocation(loc, "resource class %q must specify an intent", name)
}
if bits.Produces == nil {
return nil, fnerrors.NewWithLocation(loc, "resource class %q must specify the provided type", name)
}
return &schema.ResourceClass{
Name: name,
IntentType: parseResourceClassType(bits.Intent),
InstanceType: parseResourceClassType(bits.Produces),
}, nil
}
func parseResourceClassType(t *cueResourceClassType) *schema.ResourceClass_Type {
return &schema.ResourceClass_Type{
ProtoType: t.Type,
ProtoSource: t.Source,
}
}