Skip to content

Commit

Permalink
Utility types for YAML marshaling
Browse files Browse the repository at this point in the history
Signed-off-by: anqurvanillapy <[email protected]>
  • Loading branch information
anqurvanillapy committed Mar 16, 2023
1 parent 5224a38 commit f291eef
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 11 deletions.
21 changes: 11 additions & 10 deletions cmd/yamlchk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ func main() {

if len(files) == 0 {
flag.Usage()
log.Fatalln("no YAML files to check")
log.Fatal("no YAML files to check")
}

p, err := filepath.Abs(schemaFile)
if err != nil {
log.Panicln(err)
log.Panic(err)
}

refURL := &url.URL{Scheme: "file", Path: p}
Expand All @@ -40,23 +40,24 @@ func main() {

d, err := os.ReadFile(file)
if err != nil {
log.Panicln(err)
log.Panic(err)
}

m := make(map[string]interface{})
if err := yaml.Unmarshal(d, &m); err != nil {
log.Panicln(err)
log.Panic(err)
}

ret, err := validator.Validate(schemaLoader, validator.NewGoLoader(m))
if err != nil {
log.Panicln(err)
log.Panic(err)
}
if !ret.Valid() {
for _, err := range ret.Errors() {
log.Printf("%s\n", err)
}
log.Panicln(fmt.Sprintf("invalid file: %s", file))
if ret.Valid() {
continue
}
for _, err := range ret.Errors() {
log.Printf("%s\n", err)
}
log.Panic(fmt.Sprintf("invalid file: %s", file))
}
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/anqur/yasch
go 1.20

require (
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137
github.com/invopop/jsonschema v0.7.0
github.com/xeipuuv/gojsonschema v1.2.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
7 changes: 6 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137 h1:s6gZFSlWYmbqAuRjVTiNNhvNRfY2Wxp9nhfyel4rklc=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
Expand All @@ -8,8 +10,9 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709 h1:Ko2LQMrRU+Oy/+EDBwX7eZ2jp3C47eDBB8EIhKTun+I=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f h1:J9EGpcZtP0E/raorCMxlFGSTBrsSlaDGf3jU/qvAE2c=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
Expand All @@ -18,5 +21,7 @@ github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
54 changes: 54 additions & 0 deletions pkg/types/type.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package types

import (
"time"

"github.com/alecthomas/units"
"github.com/invopop/jsonschema"
)

type Duration struct{ Dur time.Duration }

func (Duration) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "string",
Title: "Duration",
Description: "Go-compatible duration",
}
}

func (d Duration) UnmarshalYAML(unmarshal func(interface{}) error) error {
var s string
if err := unmarshal(&s); err != nil {
return err
}
dur, err := time.ParseDuration(s)
if err != nil {
return err
}
d.Dur = dur
return nil
}

type Size struct{ Size units.Base2Bytes }

func (Size) JSONSchema() *jsonschema.Schema {
return &jsonschema.Schema{
Type: "string",
Title: "Size",
Description: "Go-compatible data size",
}
}

func (s Size) UnmarshalYAML(unmarshal func(interface{}) error) error {
var r string
if err := unmarshal(&r); err != nil {
return err
}
size, err := units.ParseBase2Bytes(r)
if err != nil {
return err
}
s.Size = size
return nil
}

0 comments on commit f291eef

Please sign in to comment.