fix(spec): properly translate v0 into v1

This commit is contained in:
sh0rez 2020-02-28 23:01:26 +01:00
parent f8be6a936f
commit e28e7ff55b
No known key found for this signature in database
GPG key ID: 87C71DF9F8181FF1
4 changed files with 99 additions and 79 deletions

View file

@ -23,7 +23,6 @@ import (
v0 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v0"
v1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1"
depsv1 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
)
const (
@ -63,39 +62,20 @@ func Unmarshal(bytes []byte) (v1.JsonnetFile, error) {
return m, err
}
if versions.Version > v1.Version {
return m, ErrUpdateJB
}
if versions.Version == v1.Version {
if err := json.Unmarshal(bytes, &m); err != nil {
return m, errors.Wrap(err, "failed to unmarshal v1 file")
}
return m, nil
} else {
switch versions.Version {
case v0.Version:
var mv0 v0.JsonnetFile
if err := json.Unmarshal(bytes, &mv0); err != nil {
return m, errors.Wrap(err, "failed to unmarshal jsonnetfile")
}
for name, dep := range mv0.Dependencies {
var d depsv1.Dependency
if dep.Source.GitSource != nil {
d = *depsv1.Parse("", dep.Source.GitSource.Remote)
d.Source.GitSource.Subdir = dep.Source.GitSource.Subdir
}
if dep.Source.LocalSource != nil {
d = *depsv1.Parse(dep.Source.LocalSource.Directory, dep.Source.GitSource.Remote)
}
d.Sum = dep.Sum
d.Version = dep.Version
m.Dependencies[name] = d
return v1.FromV0(mv0)
case v1.Version:
if err := json.Unmarshal(bytes, &m); err != nil {
return m, errors.Wrap(err, "failed to unmarshal v1 file")
}
return m, nil
default:
return m, ErrUpdateJB
}
}

View file

@ -31,58 +31,60 @@ const notExist = "/this/does/not/exist"
const v0JSON = `{
"dependencies": [
{
"name": "grafana-builder",
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "grafana-builder"
}
},
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
},
{
"name": "prometheus-mixin",
"source": {
"git": {
"remote": "https://github.com/prometheus/prometheus",
"subdir": "documentation/prometheus-mixin"
}
},
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
}
{
"name": "grafana-builder",
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "grafana-builder"
}
},
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
},
{
"name": "prometheus-mixin",
"source": {
"git": {
"remote": "https://github.com/prometheus/prometheus",
"subdir": "documentation/prometheus-mixin"
}
},
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
}
]
}`
var v0Jsonnetfile = v1.JsonnetFile{
Dependencies: map[string]deps.Dependency{
"grafana-builder": {
"github.com/grafana/jsonnet-libs/grafana-builder": {
Source: deps.Source{
GitSource: &deps.Git{
Scheme: deps.GitSchemeHTTPS,
Host: "github.com",
User: "grafana",
Repo: "jsonnet-libs",
Subdir: "grafana-builder",
Subdir: "/grafana-builder",
},
},
Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=",
Version: "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
Sum: "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE=",
LegacyNameCompat: "grafana-builder",
},
"prometheus-mixin": {
"github.com/prometheus/prometheus/documentation/prometheus-mixin": {
Source: deps.Source{
GitSource: &deps.Git{
Scheme: deps.GitSchemeHTTPS,
Host: "github.com",
User: "prometheus",
Repo: "prometheus",
Subdir: "documentation/prometheus-mixin",
Subdir: "/documentation/prometheus-mixin",
},
},
Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=",
Version: "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
Sum: "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY=",
LegacyNameCompat: "prometheus-mixin",
},
},
LegacyImports: true,
@ -91,27 +93,27 @@ var v0Jsonnetfile = v1.JsonnetFile{
const v1JSON = `{
"version": 1,
"dependencies": [
{
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "grafana-builder"
}
},
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
},
{
"name": "prometheus",
"source": {
"git": {
"remote": "https://github.com/prometheus/prometheus",
"subdir": "documentation/prometheus-mixin"
}
},
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
}
{
"source": {
"git": {
"remote": "https://github.com/grafana/jsonnet-libs",
"subdir": "grafana-builder"
}
},
"version": "54865853ebc1f901964e25a2e7a0e4d2cb6b9648",
"sum": "ELsYwK+kGdzX1mee2Yy+/b2mdO4Y503BOCDkFzwmGbE="
},
{
"name": "prometheus",
"source": {
"git": {
"remote": "https://github.com/prometheus/prometheus",
"subdir": "documentation/prometheus-mixin"
}
},
"version": "7c039a6b3b4b2a9d7c613ac8bd3fc16e8ca79684",
"sum": "bVGOsq3hLOw2irNPAS91a5dZJqQlBUNWy3pVwM4+kIY="
}
],
"legacyImports": false
}`

View file

@ -19,6 +19,8 @@ import (
"sort"
)
const Version = 0
// JsonnetFile is the structure of a `.json` file describing a set of jsonnet
// dependencies. It is used for both, the jsonnetFile and the lockFile.
type JsonnetFile struct {

36
spec/v1/v0.go Normal file
View file

@ -0,0 +1,36 @@
package spec
import (
"path/filepath"
v0 "github.com/jsonnet-bundler/jsonnet-bundler/spec/v0"
"github.com/jsonnet-bundler/jsonnet-bundler/spec/v1/deps"
)
func FromV0(mv0 v0.JsonnetFile) (JsonnetFile, error) {
m := New()
m.LegacyImports = true
for name, old := range mv0.Dependencies {
var d deps.Dependency
switch {
case old.Source.GitSource != nil:
d = *deps.Parse("", old.Source.GitSource.Remote)
subdir := filepath.Clean("/" + old.Source.GitSource.Subdir)
d.Source.GitSource.Subdir = subdir
case old.Source.LocalSource != nil:
d = *deps.Parse("", old.Source.LocalSource.Directory)
}
d.Sum = old.Sum
d.Version = old.Version
d.LegacyNameCompat = name
m.Dependencies[d.Name()] = d
}
return m, nil
}