Skip to content

Commit

Permalink
add order and order.groups to cargo,
Browse files Browse the repository at this point in the history
- pack will now preserve these fields when building archives

Signed-off-by: Ryan Moran <[email protected]>
  • Loading branch information
ForestEckhardt authored and Ryan Moran committed May 6, 2020
1 parent 4365e28 commit 342a3cf
Show file tree
Hide file tree
Showing 4 changed files with 332 additions and 132 deletions.
60 changes: 41 additions & 19 deletions cargo/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,49 @@ import (
)

type Config struct {
API string `toml:"api" json:"api"`
API string `toml:"api" json:"api"`
Buildpack ConfigBuildpack `toml:"buildpack" json:"buildpack"`
Metadata ConfigMetadata `toml:"metadata" json:"metadata"`
Stacks []ConfigStack `toml:"stacks" json:"stacks"`
Metadata ConfigMetadata `toml:"metadata" json:"metadata"`
Stacks []ConfigStack `toml:"stacks" json:"stacks"`
Order []ConfigOrder `toml:"order" json:"order"`
}

type ConfigStack struct {
ID string `toml:"id" json:"id"`
}

type ConfigBuildpack struct {
ID string `toml:"id" json:"id"`
Name string `toml:"name" json:"name"`
ID string `toml:"id" json:"id"`
Name string `toml:"name" json:"name"`
Version string `toml:"version" json:"version"`
}

type ConfigMetadata struct {
IncludeFiles []string `toml:"include_files" json:"include_files"`
IncludeFiles []string `toml:"include_files" json:"include_files"`
PrePackage string `toml:"pre_package" json:"pre_package"`
DefaultVersions map[string]string `toml:"default-versions" json:"default-versions"`
Dependencies []ConfigMetadataDependency `toml:"dependencies" json:"dependencies"`
Unstructured map[string]interface{} `toml:"-" json:"-"`
DefaultVersions map[string]string `toml:"default-versions" json:"default-versions"`
Dependencies []ConfigMetadataDependency `toml:"dependencies" json:"dependencies"`
Unstructured map[string]interface{} `toml:"-" json:"-"`
}

type ConfigMetadataDependency struct {
DeprecationDate time.Time `toml:"deprecation_date" json:"deprecation_date"`
ID string `toml:"id" json:"id"`
Name string `toml:"name" json:"name"`
SHA256 string `toml:"sha256" json:"sha256"`
Stacks []string `toml:"stacks" json:"stacks"`
URI string `toml:"uri" json:"uri"`
Version string `toml:"version" json:"version"`
ID string `toml:"id" json:"id"`
Name string `toml:"name" json:"name"`
SHA256 string `toml:"sha256" json:"sha256"`
Stacks []string `toml:"stacks" json:"stacks"`
URI string `toml:"uri" json:"uri"`
Version string `toml:"version" json:"version"`
}

type ConfigOrder struct {
Group []ConfigOrderGroup `toml:"group" json:"group"`
}

type ConfigOrderGroup struct {
ID string `toml:"id" json:"id"`
Version string `toml:"version" json:"version"`
Optional bool `toml:"optional,omitempty" json:"optional,omitempty"`
}

func EncodeConfig(writer io.Writer, config Config) error {
Expand Down Expand Up @@ -80,10 +91,21 @@ func (m ConfigMetadata) MarshalJSON() ([]byte, error) {
metadata[key] = value
}

metadata["include_files"] = m.IncludeFiles
metadata["pre_package"] = m.PrePackage
metadata["dependencies"] = m.Dependencies
metadata["default-versions"] = m.DefaultVersions
if len(m.IncludeFiles) > 0 {
metadata["include_files"] = m.IncludeFiles
}

if len(m.PrePackage) > 0 {
metadata["pre_package"] = m.PrePackage
}

if len(m.Dependencies) > 0 {
metadata["dependencies"] = m.Dependencies
}

if len(m.DefaultVersions) > 0 {
metadata["default-versions"] = m.DefaultVersions
}

return json.Marshal(metadata)
}
Expand Down
70 changes: 70 additions & 0 deletions cargo/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ func testConfig(t *testing.T, context spec.G, it spec.S) {
"some-dependency": "1.2.x",
},
},
Order: []cargo.ConfigOrder{
{
Group: []cargo.ConfigOrderGroup{
{
ID: "some-dependency",
Version: "some-version",
},
{
ID: "other-dependency",
Version: "other-version",
Optional: true,
},
},
},
},
})
Expect(err).NotTo(HaveOccurred())
Expect(buffer.String()).To(MatchTOML(`api = "0.2"
Expand Down Expand Up @@ -94,6 +109,15 @@ some-dependency = "1.2.x"
[[stacks]]
id = "some-stack-id"
[[order]]
[[order.group]]
id = "some-dependency"
version = "some-version"
[[order.group]]
id = "other-dependency"
version = "other-version"
optional = true
`))
})

Expand Down Expand Up @@ -142,6 +166,16 @@ some-dependency = "1.2.x"
[[stacks]]
id = "some-stack-id"
[[order]]
[[order.group]]
id = "some-dependency"
version = "some-version"
[[order.group]]
id = "other-dependency"
version = "other-version"
optional = true
`)

var config cargo.Config
Expand Down Expand Up @@ -179,6 +213,22 @@ some-dependency = "1.2.x"
"some-dependency": "1.2.x",
},
},
Order: []cargo.ConfigOrder{
{
Group: []cargo.ConfigOrderGroup{
{
ID: "some-dependency",
Version: "some-version",
Optional: false,
},
{
ID: "other-dependency",
Version: "other-version",
Optional: true,
},
},
},
},
}))
})

Expand All @@ -193,7 +243,27 @@ some-dependency = "1.2.x"
})

context("ConfigMetadata", func() {
context("MarshalJSON", func() {
context("when the all fields are empty", func() {
it("does not marshal any fields", func() {
var metadata cargo.ConfigMetadata
output, err := metadata.MarshalJSON()
Expect(err).NotTo(HaveOccurred())
Expect(string(output)).To(MatchJSON(`{}`))
})
})
})

context("UnmarshalJSON", func() {
context("when the all fields are empty", func() {
it("does not unmarshal any fields", func() {
var metadata cargo.ConfigMetadata
err := metadata.UnmarshalJSON([]byte(`{}`))
Expect(err).NotTo(HaveOccurred())
Expect(metadata).To(Equal(cargo.ConfigMetadata{}))
})
})

context("failure cases", func() {
context("metadata field is not a object", func() {
it("it returns an error", func() {
Expand Down
Loading

0 comments on commit 342a3cf

Please sign in to comment.