-
Notifications
You must be signed in to change notification settings - Fork 25
/
utils.go
151 lines (138 loc) · 3.79 KB
/
utils.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
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
* SPDX-License-Identifier: MIT
*/
package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/providers/config"
"github.com/eclipse-symphony/symphony/coa/pkg/apis/v1alpha2/providers/secret"
"k8s.io/client-go/util/jsonpath"
)
func UnmarshalDuration(duration string) (time.Duration, error) {
var v interface{}
if err := json.Unmarshal([]byte(duration), &v); err != nil {
return 1 * time.Millisecond, err
}
switch value := v.(type) {
case float64:
return time.Duration(value), nil
case string:
ret, err := time.ParseDuration(value)
if err != nil {
return 1 * time.Microsecond, err
}
return ret, nil
default:
return 1 * time.Microsecond, errors.New("invalid duration format")
}
}
func ParseProperty(val string) string {
if strings.HasPrefix(val, "$env:") {
return os.Getenv(val[5:])
}
return val
}
type EvaluationContext struct {
ConfigProvider config.IExtConfigProvider
SecretProvider secret.ISecretProvider
DeploymentSpec interface{}
Properties map[string]string
Inputs map[string]interface{}
Outputs map[string]map[string]interface{}
Component string
Value interface{}
Namespace string
}
func (e *EvaluationContext) Clone() *EvaluationContext {
// The Clone() method shares references to the same ConfigProvider and SecretProvider
// Other fields are not shared and need to be filled in by the caller
if e == nil {
return nil
}
return &EvaluationContext{
ConfigProvider: e.ConfigProvider,
SecretProvider: e.SecretProvider,
}
}
func JsonPathQuery(obj interface{}, jsonPath string) (interface{}, error) {
jPath := jsonPath
if !strings.HasPrefix(jPath, "{") {
jPath = "{" + jsonPath + "}" // k8s.io/client-go/util/jsonpath requires JsonPath expression to be wrapped in {}
}
result, err := jsonPathQuery(obj, jPath)
if err == nil {
return result, nil
}
// This is a workaround for filtering by root-level attributes. In this case, we need to
// wrap the object into an array and then query the array.
var arr []interface{}
switch obj.(type) {
case []interface{}:
// the object is already an array, so the query didn't work
return nil, v1alpha2.NewCOAError(nil, fmt.Sprintf("no matches found by JsonPath query '%s'", jsonPath), v1alpha2.InternalError)
default:
arr = append(arr, obj)
}
return jsonPathQuery(arr, jPath)
}
func jsonPathQuery(obj interface{}, jsonPath string) (interface{}, error) {
jpLookup := jsonpath.New("lookup")
jpLookup.AllowMissingKeys(true)
jpLookup.EnableJSONOutput(true)
err := jpLookup.Parse(jsonPath)
if err != nil {
return nil, err
}
var buf bytes.Buffer
err = jpLookup.Execute(&buf, obj)
if err != nil {
return nil, err
}
var result []interface{}
err = json.Unmarshal(buf.Bytes(), &result)
if err != nil {
return nil, err
} else if len(result) == 0 {
return nil, v1alpha2.NewCOAError(nil, fmt.Sprintf("no matches found by JsonPath query '%s'", jsonPath), v1alpha2.InternalError)
} else if len(result) == 1 {
return result[0], nil
} else {
return result, nil
}
}
func FormatAsString(val interface{}) string {
switch tv := val.(type) {
case string:
return tv
case int:
return strconv.Itoa(tv)
case int32:
return strconv.Itoa(int(tv))
case int64:
return strconv.Itoa(int(tv))
case float32:
return strconv.FormatFloat(float64(tv), 'f', -1, 32)
case float64:
return strconv.FormatFloat(tv, 'f', -1, 64)
case bool:
return strconv.FormatBool(tv)
case map[string]interface{}:
ret, _ := json.Marshal(tv)
return string(ret)
case []interface{}:
ret, _ := json.Marshal(tv)
return string(ret)
default:
return fmt.Sprintf("%v", tv)
}
}