Skip to content

Commit

Permalink
use custom type Tag for tag in config source
Browse files Browse the repository at this point in the history
since json.Number doesn't allow to decode non-numeric string anymore.
golang/go#14702

The Tag.UnmarshalJSON() function is taken from registry-image-resource so
it is good that we finally make them consistent.

Add more tests to cover this change. Nothing is expected to be broken.

Signed-off-by: Rui Yang <ryang@pivotal.io>
  • Loading branch information
Rui Yang authored and christine-malloy committed Jun 4, 2020
1 parent edeac37 commit eed3e0a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
2 changes: 1 addition & 1 deletion cmd/check/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func main() {
registryHost = registryMirrorUrl.Host
}

tag := request.Source.Tag.String()
tag := string(request.Source.Tag)
if tag == "" {
tag = "latest"
}
Expand Down
19 changes: 18 additions & 1 deletion cmd/check/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "encoding/json"

type Source struct {
Repository string `json:"repository"`
Tag json.Number `json:"tag"`
Tag Tag `json:"tag"`
Username string `json:"username"`
Password string `json:"password"`
InsecureRegistries []string `json:"insecure_registries"`
Expand Down Expand Up @@ -38,3 +38,20 @@ type ClientCertKey struct {
Cert string `json:"cert"`
Key string `json:"key"`
}

// Tag refers to a tag for an image in the registry.
type Tag string

// UnmarshalJSON accepts numeric and string values.
func (tag *Tag) UnmarshalJSON(b []byte) (err error) {
var s string
if err = json.Unmarshal(b, &s); err == nil {
*tag = Tag(s)
} else {
var n json.RawMessage
if err = json.Unmarshal(b, &n); err == nil {
*tag = Tag(n)
}
}
return err
}
24 changes: 23 additions & 1 deletion tests/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var _ = Describe("Check", func() {
session := check(map[string]interface{}{
"source": map[string]interface{}{
"repository": repository,
"tag": tag,
"tag": tag,
},
})

Expand All @@ -71,4 +71,26 @@ var _ = Describe("Check", func() {

Expect(session.Out).To(gbytes.Say(`{"digest":`))
})

It("prints out the digest for a existent image and quoted numeric tag", func() {
session := check(map[string]interface{}{
"source": map[string]interface{}{
"repository": "alpine",
"tag": "3.7",
},
})

Expect(session.Out).To(gbytes.Say(`{"digest":`))
})

It("prints out the digest for a existent image and numeric tag", func() {
session := check(map[string]interface{}{
"source": map[string]interface{}{
"repository": "alpine",
"tag": 3.7,
},
})

Expect(session.Out).To(gbytes.Say(`{"digest":`))
})
})

0 comments on commit eed3e0a

Please sign in to comment.