Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add strict option #209

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ffjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ import (

var outputPathFlag = flag.String("w", "", "Write generate code to this path instead of ${input}_ffjson.go.")
var goCmdFlag = flag.String("go-cmd", "", "Path to go command; Useful for `goapp` support.")
var strictDecodeFlag = flag.Bool("strict", false, "Returns an error when trying to decode unknow fields.")
var importNameFlag = flag.String("import-name", "", "Override import name in case it cannot be detected.")
var forceRegenerateFlag = flag.Bool("force-regenerate", false, "Regenerate every input file, without checking modification date.")
var resetFields = flag.Bool("reset-fields", false, "When unmarshalling reset all fields missing in the JSON")
var resetFields = flag.Bool("reset-fields", false, "When unmarshalling reset all fields missing in the JSON.")

func usage() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n\n", os.Args[0])
Expand Down Expand Up @@ -74,7 +75,7 @@ func main() {
importName = *importNameFlag
}

err := generator.GenerateFiles(goCmd, inputPath, outputPath, importName, *forceRegenerateFlag, *resetFields)
err := generator.GenerateFiles(goCmd, inputPath, outputPath, importName, *forceRegenerateFlag, *resetFields, *strictDecodeFlag)

if err != nil {
fmt.Fprintf(os.Stderr, "Error: %s:\n\n", err)
Expand Down
18 changes: 18 additions & 0 deletions fflib/v1/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package v1

import "fmt"

// ErrUnknowFields error is return when some unknow fields are found
type ErrUnknowFields struct {
Fields []string
}

// NewErrUnknowFields create an instance UnknowFields error
func NewErrUnknowFields(fields []string) *ErrUnknowFields {
return &ErrUnknowFields{
Fields: fields,
}
}
func (e *ErrUnknowFields) Error() string {
return fmt.Sprintf("unknow fields %v", e.Fields)
}
8 changes: 3 additions & 5 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@
package generator

import (
"errors"
"fmt"
"os"
)

func GenerateFiles(goCmd string, inputPath string, outputPath string, importName string, forceRegenerate bool, resetFields bool) error {

func GenerateFiles(goCmd string, inputPath string, outputPath string, importName string, forceRegenerate bool, resetFields bool, strict bool) error {
if _, StatErr := os.Stat(outputPath); !os.IsNotExist(StatErr) {
inputFileInfo, inputFileErr := os.Stat(inputPath)
outputFileInfo, outputFileErr := os.Stat(outputPath)
Expand All @@ -43,11 +41,11 @@ func GenerateFiles(goCmd string, inputPath string, outputPath string, importName
return err
}

im := NewInceptionMain(goCmd, inputPath, outputPath, resetFields)
im := NewInceptionMain(goCmd, inputPath, outputPath, resetFields, strict)

err = im.Generate(packageName, structs, importName)
if err != nil {
return errors.New(fmt.Sprintf("error=%v path=%q", err, im.TempMainPath))
return fmt.Errorf("error=%v path=%q", err, im.TempMainPath)
}

err = im.Run()
Expand Down
8 changes: 6 additions & 2 deletions generator/inceptionmain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions inception/decoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func CreateUnmarshalJSON(ic *Inception, si *StructInfo) error {
IC: ic,
ValidValues: validValues,
ResetFields: ic.ResetFields,
Strict: ic.Strict,
})

ic.OutputFuncs = append(ic.OutputFuncs, out)
Expand Down
14 changes: 14 additions & 0 deletions inception/decoder_tpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ type ujFunc struct {
SI *StructInfo
ValidValues []string
ResetFields bool
Strict bool
}

var ujFuncTxt = `
Expand All @@ -537,6 +538,9 @@ func (uj *{{.SI.Name}}) UnmarshalJSON(input []byte) error {
func (uj *{{.SI.Name}}) UnmarshalJSONFFLexer(fs *fflib.FFLexer, state fflib.FFParseState) error {
var err error = nil
currentKey := ffj_t_{{.SI.Name}}base
{{if eq .Strict true}}
var unknowKeys = make([]string, 0)
{{end}}
_ = currentKey
tok := fflib.FFTok_init
wantedTok := fflib.FFTok_init
Expand Down Expand Up @@ -610,6 +614,9 @@ mainparse:
goto mainparse
}
{{end}}
{{if eq .Strict true}}
unknowKeys = append(unknowKeys, string(kn))
{{end}}
currentKey = ffj_t_{{.SI.Name}}no_such_key
state = fflib.FFParse_want_colon
goto mainparse
Expand Down Expand Up @@ -696,6 +703,13 @@ done:
}
{{end}}
{{end}}

{{if eq .Strict true}}
if len(unknowKeys) > 0 {
return fs.WrapErr(fflib.NewErrUnknowFields(unknowKeys))
}
{{end}}

return nil
}
`
Expand Down
7 changes: 5 additions & 2 deletions inception/inception.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@ package ffjsoninception
import (
"errors"
"fmt"
"github.com/pquerna/ffjson/shared"
"io/ioutil"
"os"
"reflect"
"sort"

"github.com/pquerna/ffjson/shared"
)

type Inception struct {
Expand All @@ -37,9 +38,10 @@ type Inception struct {
OutputFuncs []string
q ConditionalWrite
ResetFields bool
Strict bool
}

func NewInception(inputPath string, packageName string, outputPath string, resetFields bool) *Inception {
func NewInception(inputPath string, packageName string, outputPath string, resetFields bool, strict bool) *Inception {
return &Inception{
objs: make([]*StructInfo, 0),
InputPath: inputPath,
Expand All @@ -48,6 +50,7 @@ func NewInception(inputPath string, packageName string, outputPath string, reset
OutputFuncs: make([]string, 0),
OutputImports: make(map[string]bool),
ResetFields: resetFields,
Strict: strict,
}
}

Expand Down