Skip to content
Snippets Groups Projects
Commit 00a1c746 authored by Ken Brooks's avatar Ken Brooks
Browse files

moving to jsonloader func, also using upstream ghodss/yml

parent 23c44a22
No related branches found
No related tags found
No related merge requests found
......@@ -3,9 +3,9 @@ module github.com/neilpa/yajsv
go 1.12
require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/ghodss/yaml v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/xeipuuv/gojsonschema v1.2.0
neilpa.me/go-x v0.1.0
sigs.k8s.io/yaml v1.2.0
gopkg.in/yaml.v2 v2.2.8 // indirect
)
......@@ -4,7 +4,6 @@ package main
import (
"bufio"
"bytes"
"flag"
"fmt"
"io/ioutil"
......@@ -12,14 +11,13 @@ import (
"os"
"path/filepath"
"runtime"
"github.com/ghodss/yaml"
"strings"
"sync"
"github.com/mitchellh/go-homedir"
"github.com/xeipuuv/gojsonschema"
"sigs.k8s.io/yaml"
"neilpa.me/go-x/fileuri"
)
const (
......@@ -86,47 +84,30 @@ func realMain(args []string) int {
return usageError("no JSON documents to validate")
}
// Compile target schema
sl := gojsonschema.NewSchemaLoader()
schemaUri := *schemaFlag
for _, ref := range refFlags {
for _, p := range glob(ref) {
uri := fileUri(p)
if uri == schemaUri {
if p == schemaUri {
continue
}
var loader gojsonschema.JSONLoader = nil
if strings.HasSuffix(uri, ".yaml") || strings.HasSuffix(uri, ".yml") {
valuesJSON, err := convertToJson(uri)
loader, err := jsonLoader(p)
if err != nil {
log.Fatal("unable to parse YAML schema\n", err)
}
loader = gojsonschema.NewBytesLoader(valuesJSON)
} else {
loader = gojsonschema.NewReferenceLoader(uri)
log.Fatalf("%s: unable to load schema ref: %s\n", *schemaFlag, err)
}
err := sl.AddSchemas(loader)
if err != nil {
addSchemaErr := sl.AddSchemas(loader)
if addSchemaErr != nil {
log.Fatalf("%s: invalid schema: %s\n", p, err)
}
}
}
var schemaLoader gojsonschema.JSONLoader = nil
if strings.HasSuffix(schemaUri, ".yaml") || strings.HasSuffix(schemaUri, ".yml") {
valuesJSON, err := convertToJson(schemaUri)
schemaLoader, err := jsonLoader(schemaUri)
if err != nil {
log.Fatal("unable to parse YAML schema\n", err)
}
schemaLoader = gojsonschema.NewBytesLoader(valuesJSON)
} else {
schemaLoader = gojsonschema.NewReferenceLoader(schemaUri)
log.Fatalf("%s: unable to load schema: %s\n", *schemaFlag, err)
}
//schemaLoader := gojsonschema.NewReferenceLoader(schemaUri)
schema, err := sl.Compile(schemaLoader)
if err != nil {
log.Fatalf("%s: invalid schema: %s\n", *schemaFlag, err)
......@@ -145,18 +126,11 @@ func realMain(args []string) int {
sem <- 0
defer func() { <-sem }()
var loader gojsonschema.JSONLoader = nil
if strings.HasSuffix(path, ".yaml") || strings.HasSuffix(path, ".yml") {
valuesJSON, err := convertToJson(path)
loader, err := jsonLoader(path)
if err != nil {
log.Fatal("unable to parse YAML\n", err)
log.Fatalf("%s: unable to load doc: %s\n", *schemaFlag, err)
}
loader = gojsonschema.NewBytesLoader(valuesJSON)
} else {
loader = gojsonschema.NewReferenceLoader(fileUri(path))
}
result, err := schema.Validate(loader)
switch {
case err != nil:
......@@ -201,44 +175,19 @@ func realMain(args []string) int {
return exit
}
func convertToJson(path string) ([]byte, error) {
values, err := ReadValuesFile(path)
if err != nil {
//return errors.Wrap(err, "unable to parse YAML")
return []byte{}, err
}
valuesData, err := yaml.Marshal(values)
if err != nil {
return []byte{}, err
}
valuesJSON, err := yaml.YAMLToJSON(valuesData)
func jsonLoader(path string) (gojsonschema.JSONLoader, error) {
buf, err := ioutil.ReadFile(path)
if err != nil {
return []byte{}, err
}
if bytes.Equal(valuesJSON, []byte("null")) {
valuesJSON = []byte("{}")
}
return valuesJSON, err
}
type Values map[string]interface{}
// ReadValues will parse YAML byte data into a Values.
func ReadValues(data []byte) (vals Values, err error) {
err = yaml.Unmarshal(data, &vals)
if len(vals) == 0 {
vals = Values{}
return nil, err
}
return vals, err
switch filepath.Ext(path) {
case ".yml", ".yaml":
buf, err = yaml.YAMLToJSON(buf)
}
// ReadValuesFile will parse a YAML file into a map of values.
func ReadValuesFile(filename string) (Values, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return map[string]interface{}{}, err
return nil, err
}
return ReadValues(data)
return gojsonschema.NewBytesLoader(buf), nil
}
func printUsage() {
......@@ -270,14 +219,6 @@ func usageError(msg string) int {
return 4
}
func fileUri(path string) string {
uri, err := fileuri.FromPath(path)
if err != nil {
log.Fatalf("%s: %s", path, err)
}
return uri
}
// glob is a wrapper that also resolves `~` since we may be skipping
// the shell expansion when single-quoting globs at the command line
func glob(pattern string) []string {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment