-
Notifications
You must be signed in to change notification settings - Fork 63
/
transform.go
192 lines (168 loc) · 5.74 KB
/
transform.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// SPDX-License-Identifier: Apache-2.0
//
// Copyright 2020 The Compose Specification Authors.
// Copyright 2022 Unikraft GmbH. 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.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package app
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/mattn/go-shellwords"
"github.com/mitchellh/mapstructure"
"github.com/pkg/errors"
"kraftkit.sh/kconfig"
"kraftkit.sh/unikraft/app/volume"
"kraftkit.sh/unikraft/arch"
"kraftkit.sh/unikraft/core"
"kraftkit.sh/unikraft/lib"
"kraftkit.sh/unikraft/plat"
"kraftkit.sh/unikraft/runtime"
"kraftkit.sh/unikraft/target"
"kraftkit.sh/unikraft/template"
)
// TransformerFunc defines a function to perform the actual transformation
type TransformerFunc func(context.Context, interface{}) (interface{}, error)
// Transformer defines a map to type transformer
type Transformer struct {
TypeOf reflect.Type
Func TransformerFunc
}
// Transform converts the source into the target struct with compose types
// transformer and the specified transformers if any.
func Transform(ctx context.Context, source interface{}, target interface{}, additionalTransformers ...Transformer) error {
data := mapstructure.Metadata{}
config := &mapstructure.DecoderConfig{
DecodeHook: mapstructure.ComposeDecodeHookFunc(
createTransformHook(ctx, additionalTransformers...),
mapstructure.StringToTimeDurationHookFunc(),
),
Result: target,
Metadata: &data,
ZeroFields: false,
WeaklyTypedInput: true,
MatchName: func(mapKey, fieldName string) bool {
maps := map[string]string{
"kconfig": "Configuration",
}
if f, ok := maps[mapKey]; ok && f == fieldName {
return true
} else if mapKey == strings.ToLower(fieldName) {
return true
}
return false
},
}
decoder, err := mapstructure.NewDecoder(config)
if err != nil {
return err
}
return decoder.Decode(source)
}
func createTransformHook(ctx context.Context, additionalTransformers ...Transformer) mapstructure.DecodeHookFuncType {
transforms := map[reflect.Type]TransformerFunc{
reflect.TypeOf(map[string]string{}): transformMapStringString,
reflect.TypeOf(kconfig.KeyValueMap{}): transformKConfig,
reflect.TypeOf(target.Command{}): transformCommand,
reflect.TypeOf(arch.ArchitectureConfig{}): arch.TransformFromSchema,
reflect.TypeOf(plat.PlatformConfig{}): plat.TransformFromSchema,
reflect.TypeOf(target.TargetConfig{}): target.TransformFromSchema,
reflect.TypeOf(map[string]*lib.LibraryConfig{}): lib.TransformMapFromSchema,
reflect.TypeOf(core.UnikraftConfig{}): core.TransformFromSchema,
reflect.TypeOf(runtime.Runtime{}): runtime.TransformFromSchema,
reflect.TypeOf(template.TemplateConfig{}): template.TransformFromSchema,
reflect.TypeOf(volume.VolumeConfig{}): volume.TransformFromSchema,
}
for _, transformer := range additionalTransformers {
transforms[transformer.TypeOf] = transformer.Func
}
return func(_ reflect.Type, target reflect.Type, data interface{}) (interface{}, error) {
transform, ok := transforms[target]
if !ok {
return data, nil
}
return transform(ctx, data)
}
}
func toString(value interface{}, allowNil bool) interface{} {
switch {
case value != nil:
return fmt.Sprint(value)
case allowNil:
return nil
default:
return ""
}
}
func toMapStringString(value map[string]interface{}, allowNil bool) map[string]interface{} {
output := make(map[string]interface{})
for key, value := range value {
output[key] = toString(value, allowNil)
}
return output
}
var transformMapStringString TransformerFunc = func(_ context.Context, data interface{}) (interface{}, error) {
switch value := data.(type) {
case map[string]interface{}:
return toMapStringString(value, false), nil
case map[string]string:
return value, nil
default:
return data, errors.Errorf("invalid type %T for map[string]string", value)
}
}
func transformMappingOrList(mappingOrList interface{}, sep string, allowNil bool) (interface{}, error) {
switch value := mappingOrList.(type) {
case map[string]interface{}:
return toMapStringString(value, allowNil), nil
case []interface{}:
result := make(map[string]interface{})
for _, value := range value {
key, val := transformValueToMapEntry(value.(string), sep, allowNil)
result[key] = val
}
return result, nil
}
return nil, errors.Errorf("expected a map or a list, got %T: %#v", mappingOrList, mappingOrList)
}
func transformValueToMapEntry(value string, separator string, allowNil bool) (string, interface{}) {
parts := strings.SplitN(value, separator, 2)
key := parts[0]
switch {
case len(parts) == 1 && allowNil:
return key, nil
case len(parts) == 1 && !allowNil:
return key, ""
default:
return key, parts[1]
}
}
var transformCommand TransformerFunc = func(_ context.Context, value interface{}) (interface{}, error) {
if str, ok := value.(string); ok {
return shellwords.Parse(str)
}
return value, nil
}
var transformKConfig TransformerFunc = func(_ context.Context, data interface{}) (interface{}, error) {
config, err := transformMappingOrList(data, "=", true)
if err != nil {
return nil, err
}
kconf := kconfig.KeyValueMap{}
for k, v := range config.(map[string]string) {
kconf.Set(k, v)
}
return kconf, nil
}