-
Notifications
You must be signed in to change notification settings - Fork 2
/
component.go
50 lines (40 loc) · 1.18 KB
/
component.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
package versionbundle
import (
"encoding/json"
"github.com/coreos/go-semver/semver"
"github.com/giantswarm/microerror"
)
// Component is the software component an authority provides. It describes the
// functionality of such a component being exposed by the authority. In return
// an authority guarantees to provide the components functionality.
type Component struct {
// Name is the name of the exposed component.
Name string `json:"name" yaml:"name"`
// Version is the version of the exposed component.
Version string `json:"version" yaml:"version"`
}
func (c Component) Validate() error {
if c.Name == "" {
return microerror.Maskf(invalidComponentError, "name must not be empty")
}
if c.Version == "" {
return microerror.Maskf(invalidComponentError, "version must not be empty")
}
_, err := semver.NewVersion(c.Version)
if err != nil {
return microerror.Maskf(invalidComponentError, "version parsing failed with error %#q", err)
}
return nil
}
func CopyComponents(components []Component) []Component {
raw, err := json.Marshal(components)
if err != nil {
panic(err)
}
var copy []Component
err = json.Unmarshal(raw, ©)
if err != nil {
panic(err)
}
return copy
}