-
Notifications
You must be signed in to change notification settings - Fork 64
/
transform.go
69 lines (60 loc) · 1.6 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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2022, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package runtime
import (
"context"
"fmt"
"strings"
"kraftkit.sh/kconfig"
"kraftkit.sh/unikraft/component"
)
// TransformFromSchema parses an input schema and returns an instantiated
// runtime.
func TransformFromSchema(ctx context.Context, props interface{}) (interface{}, error) {
runtime := Runtime{}
switch entry := props.(type) {
case string:
var split []string
// Is there a schema specifier?
if strings.Contains(entry, "://") {
split = strings.Split(entry, "://")
switch split[0] {
case "oci":
split = strings.Split(split[1], ":")
runtime.source = split[0]
if len(split) > 1 {
runtime.version = split[1]
}
case "kernel":
runtime.kernel = split[1]
}
} else {
// The following sequence parses the format:
split = strings.Split(entry, ":")
if len(split) > 2 {
return nil, fmt.Errorf("expected format template value to be <oci>:<tag>")
}
runtime.name = split[0]
if len(split) > 1 {
runtime.version = split[1]
}
}
case map[string]interface{}:
c, err := component.TranslateFromSchema(props)
if err != nil {
return nil, err
}
if source, ok := c["source"]; ok {
runtime.source = source.(string)
}
if version, ok := c["version"]; ok {
runtime.version = version.(string)
}
if kconf, ok := c["kconfig"]; ok {
runtime.kconfig = kconf.(kconfig.KeyValueMap)
}
}
return runtime, nil
}