Coder Social home page Coder Social logo

loopfz / gadgeto Goto Github PK

View Code? Open in Web Editor NEW
56.0 56.0 49.0 191 KB

Gadgeto! is a collection of tools that aim to facilitate the development of REST APIs in Go.

License: MIT License

Go 100.00%
golang golang-library http mock rest-api test-framework

gadgeto's People

Contributors

alkorin avatar fsamin avatar gcnathan avatar geoffreybauduin avatar hmajid2301 avatar inozuma avatar loopfz avatar maxatome avatar pablito-perez avatar rbeuque74 avatar reenjii avatar wi2l avatar ybriffa avatar yesnault avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

gadgeto's Issues

How to add RegisterTagNameFunc using Tonic

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.

Upgrade validator dependency to v10

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.

How to do custom binding for uuid?

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"
}

HeaderTag now working in input struct

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

Bind keys from gin.Context

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

Update DefaultRenderHook to use current ResponseWriter status, if any.

func DefaultRenderHook(c *gin.Context, status int, payload interface{}) {

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 ?

nil dereference during RegisterTagNameFunc

There is no initValidator() here:

func RegisterTagNameFunc(registerTagFunc validator.TagNameFunc) {
	validatorObj.RegisterTagNameFunc(registerTagFunc)
}

so a panic occurs.

Support RegisterValidationCtx()

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.

fatal error: concurrent map writes

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.