-
Notifications
You must be signed in to change notification settings - Fork 0
/
function.go
154 lines (126 loc) · 3.46 KB
/
function.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2023 The fhub-runtime-go Authors
// This file is part of fhub-runtime-go.
//
// This file is part of fhub-runtime-go.
// fhub-runtime-go is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// fhub-runtime-go is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with fhub-runtime-go. If not, see <https://www.gnu.org/licenses/>.
package model
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"cuelang.org/go/cue"
"cuelang.org/go/cue/cuecontext"
)
type Function struct {
inputValue cue.Value `fhub:"input" fhub-unmarshal:"true"`
outputValue cue.Value `fhub:"output" fhub-unmarshal:"true"`
Inputs []string `validate:"min=1"`
Outputs []string `validate:"min=1"`
}
func (f *Function) Unmarshal(field string, value cue.Value) (err error) {
if field == "input" {
f.inputValue = value
f.Inputs, err = type_struct(f.inputValue)
if err != nil {
return err
}
} else if field == "output" {
f.outputValue = value
f.Outputs, err = type_struct(f.outputValue)
if err != nil {
return err
}
} else {
return errors.New("invalid field")
}
return nil
}
func (f Function) UnmarshalInput(data []byte) (map[string]any, error) {
return unmarshalToMap(data, f.inputValue)
}
func (f Function) UnmarshalOutput(data []byte) (map[string]any, error) {
return unmarshalToMap(data, f.outputValue)
}
func (f Function) ValidateInput(data []byte) error {
return validate(data, f.inputValue)
}
func (f Function) ValidateOutput(data []byte) error {
return validate(data, f.outputValue)
}
type Parameter struct {
Label string
cueType cue.Kind
}
func (f *Parameter) Type(k reflect.Kind) bool {
switch k {
case reflect.String:
return f.cueType.IsAnyOf(cue.StringKind)
case reflect.Bool:
return f.cueType.IsAnyOf(cue.StringKind)
}
return false
}
func type_struct(value cue.Value) ([]string, error) {
fields, err := value.Fields()
if err != nil {
return nil, err
}
parameters := []string{}
for fields.Next() {
parameters = append(parameters, fields.Label())
}
return parameters, nil
}
func unmarshalToMap(data []byte, value cue.Value) (map[string]any, error) {
valueUnify, err := unifyDataAndValue(data, value)
if err != nil {
return nil, fmt.Errorf("error in cue value unify: %s", err)
}
dataMap := map[string]any{}
fields, err := valueUnify.Fields()
if err != nil {
return nil, err
}
for fields.Next() {
var value any
fields.Value().Decode(&value)
dataMap[fields.Label()] = value
}
return dataMap, nil
}
func validate(data []byte, value cue.Value) error {
valueUnify, err := unifyDataAndValue(data, value)
if err != nil {
return err
}
return valueUnify.Validate(
cue.Attributes(true),
cue.Optional(true),
cue.Hidden(true),
cue.Concrete(true),
)
}
func unifyDataAndValue(data []byte, value cue.Value) (cue.Value, error) {
valid := json.Valid(data)
if !valid {
return value, nil
}
dataValue := cuecontext.New().CompileBytes(data)
valueUnify := value.UnifyAccept(dataValue, value)
if valueUnify.Err() != nil {
return value, valueUnify.Err()
}
return valueUnify, nil
}