refactor: use a map instead of a slice

Packages are unique anyways so it makes sense to use a map to avoid having
duplicates.

For compatibility reasons custom json (un)marshallers hide this change from the
end user
This commit is contained in:
sh0rez 2019-10-16 17:36:25 +02:00
parent 4b6e2d89e9
commit 6e3e7b2fdd
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1
4 changed files with 52 additions and 17 deletions

View file

@ -14,10 +14,42 @@
package spec
import (
"encoding/json"
"sort"
)
type JsonnetFile struct {
Dependencies map[string]Dependency
}
type jsonFile struct {
Dependencies []Dependency `json:"dependencies"`
}
func (jf *JsonnetFile) UnmarshalJSON(data []byte) error {
var s jsonFile
if err := json.Unmarshal(data, &s); err != nil {
return err
}
jf.Dependencies = make(map[string]Dependency)
for _, d := range s.Dependencies {
jf.Dependencies[d.Name] = d
}
return nil
}
func (jf JsonnetFile) MarshalJSON() ([]byte, error) {
var s jsonFile
for _, d := range jf.Dependencies {
s.Dependencies = append(s.Dependencies, d)
}
return json.Marshal(s)
}
type Dependency struct {
Name string `json:"name"`
Source Source `json:"source"`