-
Notifications
You must be signed in to change notification settings - Fork 23
/
imports.go
112 lines (101 loc) · 3.97 KB
/
imports.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
// Copyright 2018 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
//
// 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 tosca
import (
"github.com/pkg/errors"
)
// An ImportDefinition is the representation of a TOSCA Import Definition
//
// See http://docs.oasis-open.org/tosca/TOSCA-Simple-Profile-YAML/v1.2/TOSCA-Simple-Profile-YAML-v1.2.html#DEFN_ELEMENT_IMPORT_DEF for more details
type ImportDefinition struct {
File string `yaml:"file" json:"file"` // Required
Repository string `yaml:"repository,omitempty" json:"repository,omitempty"`
NamespaceURI string `yaml:"namespace_uri,omitempty" json:"namespace_uri,omitempty"` // Deprecated
NamespacePrefix string `yaml:"namespace_prefix,omitempty" json:"namespace_prefix,omitempty"`
}
// UnmarshalYAML unmarshals a yaml into an ImportDefinition
func (i *ImportDefinition) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
// First case, single-line import providing a URI.
// Example of imports using this format:
// imports:
// - path/file.yaml
if err := unmarshal(&s); err == nil {
i.File = s
return nil
}
// Second case, multi-line import.
// Example of imports using this format:
// imports:
// - file: path/file.yaml
// repository: myrepository
// namespace_uri: http://mycompany.com/tosca/1.0/platform
// namespace_prefix: mycompany
var str struct {
File string `yaml:"file"`
Repository string `yaml:"repository,omitempty"`
NamespaceURI string `yaml:"namespace_uri,omitempty"`
NamespacePrefix string `yaml:"namespace_prefix,omitempty"`
}
var unmarshalError error
if unmarshalError = unmarshal(&str); unmarshalError == nil {
if str.File != "" {
i.File = str.File
i.Repository = str.Repository
i.NamespaceURI = str.NamespaceURI
i.NamespacePrefix = str.NamespacePrefix
return nil
}
// This entry is not compatible with the latest TOSCA specification.
// But the error is not returned yet, if ever the entry is
// compatible with a previous TOSCA specification
unmarshalError = errors.New("Missing required key 'file' in import definition")
}
// Additional case for backward compatibility with TOSCA 1.0 specification:
// Multi-line import with an import name.
// Example of imports using this format:
// imports:
// - my_import-name:
// file: path/file.yaml
// repository: myrepository
// namespace_uri: http://mycompany.com/tosca/1.0/platform
// namespace_prefix: mycompany
var importMap map[string]ImportDefinition
if err := unmarshal(&importMap); err == nil && len(importMap) == 1 {
// Iterate over a map of one element
for _, importDefinition := range importMap {
i.File = importDefinition.File
i.Repository = importDefinition.Repository
i.NamespaceURI = importDefinition.NamespaceURI
i.NamespacePrefix = importDefinition.NamespacePrefix
}
return nil
}
// Additional case for backward compatibility with TOSCA 1.0 specification:
// Single-line import with an import name.
// Example of imports using this format:
// imports:
// - my_import-name: path/file.yaml
var stringMap map[string]string
if err := unmarshal(&stringMap); err == nil && len(stringMap) == 1 {
// Iterate over a map of one element
for _, value := range stringMap {
i.File = value
}
return nil
}
// No match for this entry, returning the unmarshal error that was found
// when attempting to match the latest TOSCA specification
return unmarshalError
}