Sfoglia il codice sorgente

Updates and refactors the parsing and usage of the source tag param to handle ints and strings

Dwayne Forde 7 anni fa
parent
commit
d4aa32055d
6 ha cambiato i file con 73 aggiunte e 14 eliminazioni
  1. 4 4
      check_test.go
  2. 1 3
      cmd/check/main.go
  3. 1 1
      cmd/in/main.go
  4. 1 1
      cmd/out/main.go
  5. 25 5
      types.go
  6. 41 0
      types_test.go

+ 4 - 4
check_test.go

@@ -42,7 +42,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = nil
@@ -59,7 +59,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = &resource.Version{
@@ -78,7 +78,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = &resource.Version{
@@ -99,7 +99,7 @@ var _ = Describe("Check", func() {
 		BeforeEach(func() {
 			req.Source = resource.Source{
 				Repository: "concourse/test-image-static",
-				RawTag:     "latest",
+				Tag:        "latest",
 			}
 
 			req.Version = &resource.Version{

+ 1 - 3
cmd/check/main.go

@@ -33,9 +33,7 @@ func main() {
 		return
 	}
 
-	ref := req.Source.Repository + ":" + req.Source.Tag()
-
-	n, err := name.ParseReference(ref, name.WeakValidation)
+	n, err := name.ParseReference(req.Source.Name(), name.WeakValidation)
 	if err != nil {
 		logrus.Errorf("could not resolve repository/tag reference: %s", err)
 		os.Exit(1)

+ 1 - 1
cmd/in/main.go

@@ -112,7 +112,7 @@ func saveDigest(dest string, image v1.Image) error {
 }
 
 func ociFormat(dest string, req InRequest, image v1.Image) {
-	tag, err := name.NewTag(req.Source.Repository+":"+req.Source.Tag(), name.WeakValidation)
+	tag, err := name.NewTag(req.Source.Name(), name.WeakValidation)
 	if err != nil {
 		logrus.Errorf("failed to construct tag reference: %s", err)
 		os.Exit(1)

+ 1 - 1
cmd/out/main.go

@@ -58,7 +58,7 @@ func main() {
 
 	logrus.Warnln("'put' is experimental, untested, and subject to change!")
 
-	ref := req.Source.Repository + ":" + req.Source.Tag()
+	ref := req.Source.Name()
 
 	n, err := name.ParseReference(ref, name.WeakValidation)
 	if err != nil {

+ 25 - 5
types.go

@@ -1,10 +1,15 @@
 package resource
 
+import (
+	"encoding/json"
+	"fmt"
+)
+
 const DefaultTag = "latest"
 
 type Source struct {
 	Repository string `json:"repository"`
-	RawTag     string `json:"tag"`
+	Tag        Tag    `json:"tag"`
 
 	Username string `json:"username"`
 	Password string `json:"password"`
@@ -12,12 +17,27 @@ type Source struct {
 	Debug bool `json:"debug"`
 }
 
-func (s Source) Tag() string {
-	if s.RawTag == "" {
-		return DefaultTag
+func (source *Source) Name() string {
+	return fmt.Sprintf("%s:%s", source.Repository, source.Tag)
+}
+
+type Tag string
+
+func (tag *Tag) UnmarshalJSON(b []byte) error {
+	var n json.Number
+
+	err := json.Unmarshal(b, &n)
+	if err != nil {
+		return err
+	}
+
+	if n.String() == "" {
+		*tag = Tag(DefaultTag)
+		return nil
 	}
 
-	return s.RawTag
+	*tag = Tag(n.String())
+	return nil
 }
 
 type Version struct {

+ 41 - 0
types_test.go

@@ -0,0 +1,41 @@
+package resource_test
+
+import (
+	"encoding/json"
+	"strings"
+
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+
+	resource "github.com/concourse/registry-image-resource"
+)
+
+var _ = Describe("Source", func() {
+	It("should unmarshal tag int value into a string", func() {
+		var source resource.Source
+		raw := []byte(`{ "tag": 0 }`)
+
+		err := json.Unmarshal(raw, &source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(source.Tag).To(Equal(resource.Tag("0")))
+	})
+
+	It("should unmarshal tag '' value to latest", func() {
+		var source resource.Source
+		raw := []byte(`{ "tag": "" }`)
+
+		err := json.Unmarshal(raw, &source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(source.Tag).To(Equal(resource.Tag("latest")))
+	})
+
+	It("should marshal a tag back out to a string", func() {
+		source := resource.Source{
+			Tag: "0",
+		}
+
+		json, err := json.Marshal(source)
+		Expect(err).ToNot(HaveOccurred())
+		Expect(strings.Contains(string(json[:]), `"tag":"0"`)).To(BeTrue())
+	})
+})