loopfz / gadgeto Goto Github PK
View Code? Open in Web Editor NEWGadgeto! is a collection of tools that aim to facilitate the development of REST APIs in Go.
License: MIT License
Gadgeto! is a collection of tools that aim to facilitate the development of REST APIs in Go.
License: MIT License
This issue has been opened automaticaly because we think you are using satori/uuid in your code. Unfortunately, satori/uuid has a critical defect where it doesn't generate random UUIDV4s satori/go.uuid#73 and should be replaced as the maintainer has been unresponsive for 5 months with the bug still open. Alternative packages you should consider: https://github.com/google/uuid https://github.com/pborman/uuid
Relevant issue I created on the wrong repo (for more context): wI2L/fizz#46
What is the best way to match the functionality if I update the function initValidator
from handler.go
func initValidator() {
validatorOnce.Do(func() {
validatorObj = validator.New()
validatorObj.SetTagName(ValidationTag)
validatorObj.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
})
}
When I do e.(tonic.BindError).ValidationErrors().Field()
I get the JSON struct tag name (i.e. rules_url
) vs the struct field name (RulesURL
).
What is the correct way to register a tag name function using tonic ? Thanks.
Hello! Is this still maintained? We really like Fizz, but can't continue to support it if Fizz is not. Thanks!
Gin has moved towards validator v10, but tonic has not. Because of this, it prevents tonic.RegisterValidation
from working properly in Go 1.16, especially since validator v9 does not expose a go.mod
file.
Upgrade should be straight forward.
Type: https://pkg.go.dev/github.com/google/uuid
Sample struct:
type ProjectByIdWire struct {
Id uuid.UUID `path:"id"`
}
I would like to change the default binding for that type to use Parse(s string) (UUID, error)
https://github.com/google/uuid/blob/v1.3.0/uuid.go#L64
Unfortunately I'm not sure how to do this. I'm using Fizz as well and changing the data type for the schema was straight forward:
fizz.Generator().OverrideDataType(reflect.TypeOf(&uuid.UUID{}), "string", "uuid")
The error I get when trying to bind using the defaults
{
"error": "binding error on field 'Id' of type 'ProjectByIdWire': parameter expect 16 values, got 1"
}
Hi, I try to use fizz to work with tonic, but I found HeaderTag not working for input struct. Because fizz not support OpenAPI security, so I tried to use header tag to some goal.
For example:
type Info struct {
Authorization string `header:"Authorization" description:"token" example:"Bear xxxxxxxx"`
Name string `json:"name" validate:"required" example:"banana"`
Origin string `json:"origin" validate:"required" description:"Country of origin of the fruit" enum:"ecuador,france,senegal,china,spain"`
Price float64 `json:"price" validate:"required" description:"Price in euros" example:"5.13"`
AddedAt time.Time `json:"-" binding:"-" description:"Date of addition of the fruit to the market"`
}
The result become to :
RestInfo:
type: object
properties:
Authorization:
type: string
description: Token
example: Bear xxxxxxxx
name:
type: string
example: banana
origin:
type: string
description: Country of origin of the fruit
enum:
- ecuador
- france
- senegal
- china
- spain
price:
type: number
description: Price in euros
format: double
example: 5.13
required:
- name
- origin
- price
Hi, guys!
I wanna bind params from gin.Context. Then I add a new tag context
, but I doubt it's a good solution. Please give me some suggestions.
My PR as follows #89
Line 101 in 775b39d
The current RenderHook
uses the default route status code that is declared when binding a handler with tonic. I would like to be able in my handler to override this default status code (say 200 OK
is default, but 204 No Content
is also possible).
Gin does provide access to the gin.ResponseWriter
via the context, so we could check if the status has been overrided by the handler and use it instead of the default.
Implementation would look like:
func DefaultRenderHook(c *gin.Context, statusCode int, payload interface{}) {
status := c.Writer.Status()
if status == 0 {
code = statusCode // use default provided by Tonic
}
if payload != nil {
if gin.IsDebugging() {
c.IndentedJSON(status, payload)
} else {
c.JSON(status, payload)
}
} else {
c.Status(status)
}
}
Thoughts @rbeuque74 @loopfz ?
The new validator package (v9) introduces the oneof
tag: https://godoc.org/gopkg.in/go-playground/validator.v9#hdr-One_Of
There is no more need to document the tonic
enum tag IMO.
As said in title, permit custom HTTP headers in iffy tests calls
There is no initValidator()
here:
func RegisterTagNameFunc(registerTagFunc validator.TagNameFunc) {
validatorObj.RegisterTagNameFunc(registerTagFunc)
}
so a panic occurs.
In validator.v10, apart from using RegisterValidation() to register validator.Func, it could also use RegisterValidationCtx() to register validator.FuncCtx. This enables you to validate parameters using an additional context.Context that is passed in.
However, this does not yet enable passing gin.Context to validator.FuncCtx. When validating the parameters, it is necessary to replace validator.Validate.Struct() with validator.Validate.StructCtx() and pass down *gin.Context.
When tonic is used inside unit tests, multiple gin instance can register routes at the same time, calling tonic/handler.go func Handler, which is not thread safe.
Example:
fatal error: concurrent map writes
goroutine 1 [running]:
runtime.throw(0xe06541, 0x15)
/home/romain/go/src/runtime/panic.go:774 +0x72 fp=0xc0004fd848 sp=0xc0004fd818 pc=0x431412
runtime.mapassign_faststr(0xcdffc0, 0xc00030b920, 0xc0005f61e0, 0x49, 0xf8aa80)
/home/romain/go/src/runtime/map_faststr.go:211 +0x417 fp=0xc0004fd8b0 sp=0xc0004fd848 pc=0x4150d7
github.com/loopfz/gadgeto/tonic.Handler(0xcd2940, 0xe34ef8, 0xc8, 0x0, 0x0, 0x0, 0x0)
/home/romain/gopath/src/github.com/ovh/utask/vendor/github.com/loopfz/gadgeto/tonic/handler.go:132 +0x5cf fp=0xc0004fda70 sp=0xc0004fd8b0 pc=0xbb604f
github.com/ovh/utask/api.(*Server).build(0xc0001ac940, 0xf73c80, 0xc0000ca010)
/home/romain/gopath/src/github.com/ovh/utask/api/server.go:312 +0x230e fp=0xc0004fdda8 sp=0xc0004fda70 pc=0xc0d1fe
github.com/ovh/utask/api.(*Server).Handler(...)
/home/romain/gopath/src/github.com/ovh/utask/api/server.go:112
github.com/ovh/utask/api_test.TestMain(0xc0001c6380)
/home/romain/gopath/src/github.com/ovh/utask/api/api_test.go:81 +0x37d fp=0xc0004fdef8 sp=0xc0004fdda8 pc=0xc17f7d
main.main()
_testmain.go:58 +0x135 fp=0xc0004fdf60 sp=0xc0004fdef8 pc=0xc20995
runtime.main()
/home/romain/go/src/runtime/proc.go:203 +0x21e fp=0xc0004fdfe0 sp=0xc0004fdf60 pc=0x432dae
runtime.goexit()
/home/romain/go/src/runtime/asm_amd64.s:1357 +0x1 fp=0xc0004fdfe8 sp=0xc0004fdfe0 pc=0x461391
goroutine 5 [syscall]:
os/signal.signal_recv(0x461396)
/home/romain/go/src/runtime/sigqueue.go:147 +0x9c
os/signal.loop()
/home/romain/go/src/os/signal/signal_unix.go:23 +0x22
created by os/signal.init.0
/home/romain/go/src/os/signal/signal_unix.go:29 +0x41
goroutine 41 [chan receive (nil chan)]:
github.com/ovh/utask/engine.Init.func2(0xf73c80, 0xc0000ca010)
/home/romain/gopath/src/github.com/ovh/utask/engine/engine.go:95 +0x48
created by github.com/ovh/utask/engine.Init
/home/romain/gopath/src/github.com/ovh/utask/engine/engine.go:94 +0x3bb
goroutine 30 [runnable]:
github.com/loopfz/gadgeto/tonic.Handler(0xceedc0, 0xe34f78, 0xc8, 0x0, 0x0, 0x0, 0x0)
/home/romain/gopath/src/github.com/ovh/utask/vendor/github.com/loopfz/gadgeto/tonic/handler.go:132 +0x5cf
github.com/ovh/utask/api.(*Server).build(0xc0001ac940, 0xf73c40, 0xc0001acbc0)
/home/romain/gopath/src/github.com/ovh/utask/api/server.go:184 +0xc09
github.com/ovh/utask/api.(*Server).ListenAndServe(0xc0001ac940, 0x0, 0x0)
/home/romain/gopath/src/github.com/ovh/utask/api/server.go:89 +0x84
created by github.com/ovh/utask/api_test.TestMain
/home/romain/gopath/src/github.com/ovh/utask/api/api_test.go:74 +0x271
func Handler is adding routes to a map, in order to be collected in tonic.go func GetRoutes
I guess that registering routes on different gin.Engine will register routes on the same map, and will probably create some inconsistency.
I will open a pull-request to fix the panic first, but I guess another bug fix should be open regarding the routes registering inconsistency.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.