Skip to content

Commit

Permalink
feat: use jsoniter fork
Browse files Browse the repository at this point in the history
  • Loading branch information
OnFireByte committed Jan 5, 2024
1 parent b5ff22a commit 83683bc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 8 deletions.
10 changes: 9 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,12 @@ module github.com/onfirebyte/glutys

go 1.21.2

require github.com/dave/jennifer v1.7.0
require (
github.com/dave/jennifer v1.7.0
github.com/onfirebyte/jsoniter v1.1.0
)

require (
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
)
43 changes: 43 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,45 @@
github.com/Masterminds/semver v1.5.0/go.mod h1:MB6lktGJrhw8PrUyiEoblNEGEQ+RzHPF078ddwwvV3Y=
github.com/Masterminds/vcs v1.13.3/go.mod h1:TiE7xuEjl1N4j016moRd6vezp6e6Lz23gypeXfzXeW8=
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
github.com/boltdb/bolt v1.1.0/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/boltdb/bolt v1.3.1/go.mod h1:clJnj/oiGkjum5o1McbSZDSLxVThjynRyGBgiAx27Ps=
github.com/dave/jennifer v1.7.0 h1:uRbSBH9UTS64yXbh4FrMHfgfY762RD+C7bUPKODpSJE=
github.com/dave/jennifer v1.7.0/go.mod h1:nXbxhEmQfOZhWml3D1cDK5M1FLnMSozpbFN/m3RmGZc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/dep v0.5.4/go.mod h1:6RZ2Wai7dSWk7qL55sDYk+8UPFqcW7all2KDBraPPFA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jmank88/nuts v0.4.0/go.mod h1:TKOSbm0p73pdAzgQ7lcZheG2cinZiXqy60KM5ooL3j8=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
github.com/onfirebyte/jsoniter v1.1.0 h1:7JaxQRsCvH+9mzDuHp4Hcpx0L77FIn8gg9NSaewOjy4=
github.com/onfirebyte/jsoniter v1.1.0/go.mod h1:KdxaOWgW4Vx46lDEJYILzBvdv5/5FbqMbYNsDFvPSuM=
github.com/pelletier/go-toml v1.9.5/go.mod h1:u1nR/EPcESfeI/szUZKdtJ0xRNbUoANCkoOuaOx1Y+c=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sdboyer/constext v0.0.0-20170321163424-836a14457353/go.mod h1:5HStXbIikwtDAgAIqiQIqVgMn7mlvZa6PTpwiAVYGYg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
30 changes: 23 additions & 7 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func (g *Builder) buildHandler() string {

paths := []string{}

generateInitFunc(f, g)
generateHandlerStruct(f, g)

for path, procedures := range routes {
Expand Down Expand Up @@ -129,14 +130,14 @@ func (g *Builder) buildHandler() string {
Id("w").Dot("Header").Call().Dot("Set").Call(Lit("Content-Type"), Lit("application/json")),
// parse body
Id("body").Op(":=").Qual("github.com/onfirebyte/glutys", "RequestBody").Values(),
Id("err").Op(":=").Qual("encoding/json", "NewDecoder").Call(Id("r").Dot("Body")).Dot("Decode").Call(Op("&").Id("body")),
Id("err").Op(":=").Id("json").Dot("NewDecoder").Call(Id("r").Dot("Body")).Dot("Decode").Call(Op("&").Id("body")),
If(Id("err").Op("!=").Nil()).Block(
Id("response").Op(":=").Map(String()).Interface().Values(Dict{
Lit("error"): Lit("Bad Request"),
Lit("msg"): Lit("Invalid JSON"),
}),
Id("w").Dot("WriteHeader").Call(Qual("net/http", "StatusBadRequest")),
Qual("encoding/json", "NewEncoder").Call(Id("w")).Dot("Encode").Call(Id("response")),
Id("json").Dot("NewEncoder").Call(Id("w")).Dot("Encode").Call(Id("response")),
),

Switch(Id("body").Dot("Method")).Block(handlerCases...),
Expand All @@ -145,6 +146,21 @@ func (g *Builder) buildHandler() string {
return f.GoString()
}

func generateInitFunc(
f *File,
g *Builder,
) {
f.Var().Id("json").Qual("github.com/onfirebyte/jsoniter", "API")
f.Func().Id("init").Params().Block(
Id("json").Op("=").Qual("github.com/onfirebyte/jsoniter", "Config").Block(
Id("EscapeHTML").Op(":").True().Op(","),
Id("SortMapKeys").Op(":").True().Op(","),
Id("ValidateJsonRawMessage").Op(":").True().Op(","),
Id("EmptyCollections").Op(":").True().Op(","),
).Dot("Froze").Call(),
)
}

func generateHandlerStruct(
f *File,
g *Builder,
Expand Down Expand Up @@ -228,7 +244,7 @@ func generateHandlerFunction(
Lit("msg"): Id("err" + argName).Dot("Error").Call(),
}),
Id("w").Dot("WriteHeader").Call(Qual("net/http", "StatusBadRequest")),
Qual("encoding/json", "NewEncoder").Call(Id("w")).Dot("Encode").Call(Id("response")),
Id("json").Dot("NewEncoder").Call(Id("w")).Dot("Encode").Call(Id("response")),
Return(),
))
}
Expand Down Expand Up @@ -271,7 +287,7 @@ func generateHandlerFunction(

varDeclare := Var().Id(argName).Add(util.GetJenType(argType))

marshaled := Id("err"+argName).Op(":=").Qual("encoding/json", "Unmarshal").Call(
marshaled := Id("err"+argName).Op(":=").Id("json").Dot("Unmarshal").Call(
Id("body").Dot("Args").Index(Lit(argPos)),
Op("&").Id(argName),
)
Expand All @@ -282,7 +298,7 @@ func generateHandlerFunction(
Lit("msg"): Id("err" + argName).Dot("Error").Call(),
}),
Id("w").Dot("WriteHeader").Call(Qual("net/http", "StatusBadRequest")),
Qual("encoding/json", "NewEncoder").Call(Id("w")).Dot("Encode").Call(Id("response")),
Id("json").Dot("NewEncoder").Call(Id("w")).Dot("Encode").Call(Id("response")),
Return(),
)

Expand Down Expand Up @@ -317,7 +333,7 @@ func generateHandlerFunction(
if hasErr {
blocks = append(blocks, If(Id("err").Op("!=").Nil()).Block(
Id("w").Dot("WriteHeader").Call(Qual("net/http", "StatusBadRequest")),
Qual("encoding/json", "NewEncoder").Call(Id("w")).Dot("Encode").Call(
Id("json").Dot("NewEncoder").Call(Id("w")).Dot("Encode").Call(
Map(String()).Interface().Values(Dict{
Lit("error"): Lit("Bad Request"),
Lit("msg"): Id("err").Dot("Error").Call(),
Expand All @@ -329,7 +345,7 @@ func generateHandlerFunction(

blocks = append(blocks,
Id("w").Dot("WriteHeader").Call(Qual("net/http", "StatusOK")),
Qual("encoding/json", "NewEncoder").Call(Id("w")).Dot("Encode").Call(
Id("json").Dot("NewEncoder").Call(Id("w")).Dot("Encode").Call(
Id("res")),
Return())

Expand Down
8 changes: 8 additions & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//go:build tools
// +build tools

package glutys

import (
_ "github.com/onfirebyte/jsoniter"
)

0 comments on commit 83683bc

Please sign in to comment.