mirror of
https://github.com/TECHNOFAB11/jsonnet-bundler.git
synced 2025-12-11 23:50:05 +01:00
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:
parent
4b6e2d89e9
commit
6e3e7b2fdd
4 changed files with 52 additions and 17 deletions
32
spec/spec.go
32
spec/spec.go
|
|
@ -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"`
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue