From 056a9afa951dc2ef6d4db087e2f632cbb73d4b20 Mon Sep 17 00:00:00 2001 From: Adam Lehechka <42357034+alehechka@users.noreply.github.com> Date: Sun, 26 Jun 2022 10:49:39 -0500 Subject: [PATCH] add ReadBytes method to Gen --- cmd/generate.go | 2 +- gen/constants.go | 3 +++ gen/gen.go | 7 +++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cmd/generate.go b/cmd/generate.go index 001c2ef..81f1353 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -34,7 +34,7 @@ var generateFlags = []cli.Flag{ Name: rootFlag, Aliases: []string{"r"}, Usage: "The name of the root object or array", - Value: "Root", + Value: gen.DefaultRootName, }, &cli.StringFlag{ Name: packageFlag, diff --git a/gen/constants.go b/gen/constants.go index e38dc29..b84a40f 100644 --- a/gen/constants.go +++ b/gen/constants.go @@ -5,3 +5,6 @@ const DefaultOutputFile = "types.go" // DefaultPackage is the name of the default package const DefaultPackage = "main" + +// DefaultRootName is the default name of the top-level JSON object +const DefaultRootName = "Root" diff --git a/gen/gen.go b/gen/gen.go index fbe5097..5315782 100644 --- a/gen/gen.go +++ b/gen/gen.go @@ -3,6 +3,7 @@ package gen import ( "encoding/json" "errors" + "io" "io/ioutil" "log" "os" @@ -69,6 +70,12 @@ func (g *Gen) SetBytes(bytes []byte) *Gen { return g } +// ReadBytes allows for the explicit setting of raw bytes via io.Reader +func (g *Gen) ReadBytes(r io.Reader) *Gen { + g.bytes, _ = ioutil.ReadAll(r) + return g +} + // SetJSON allows for the explicit setting of provided JSON payload // TODO: Needs further testing before ready for external usage. func (g *Gen) setJSON(json interface{}) *Gen {