From 44dbcc5da2733f732090c530c3a1c25b4f618721 Mon Sep 17 00:00:00 2001 From: Ashish Bhatia Date: Tue, 18 Oct 2022 15:26:04 -0700 Subject: [PATCH] Update README Add code blocks to README --- tonic/README | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tonic/README b/tonic/README index 860f82f..a50b47e 100644 --- a/tonic/README +++ b/tonic/README @@ -8,11 +8,13 @@ of various parameters. Here is an example input object. +```go type MyInput struct { Foo int `path:"foo"` Bar string `query:"bar" default:"foobar"` Baz string `json:"baz"` } +``` Output objects can be of any type, and will be marshaled to JSON. @@ -20,24 +22,28 @@ Input validation is performed after binding into the object using the validator (https://github.com/go-playground/validator). You can use the tag 'validate' on your object definition to perform validation inside the tonic handler phase. +```go type MyInput struct { Foo int `path:"foo" validate:"required,gt=10"` Bar string `query:"bar" default:"foobar" validate:"nefield=Baz"` Baz string `json:"baz" validate:"required,email"` } +``` enum input validation is also implemented natively by tonic, and can check that the provided input value corresponds to one of the expected enum values. +```go type MyInput struct { Bar string `query:"bar" enum:"foo,buz,biz"` } - +``` The handler can return an error, which will be returned to the caller. Here is a basic application that greets a user on http://localhost:8080/hello/me +```go import ( "errors" "fmt" @@ -66,7 +72,7 @@ Here is a basic application that greets a user on http://localhost:8080/hello/me r.GET("/hello/:name", tonic.Handler(GreetUser, 200)) r.Run(":8080") } - +``` If needed, you can also override different parts of the logic via certain available hooks in tonic: - binding @@ -82,6 +88,7 @@ We provide a ready-to-use error hook that depends on the juju/errors package (ri Example of the same application as before, using juju errors: +```go import ( "fmt" @@ -112,6 +119,6 @@ Example of the same application as before, using juju errors: r.GET("/hello/:name", tonic.Handler(GreetUser, 200)) r.Run(":8080") } - +``` You can also easily serve auto-generated swagger documentation (using tonic data) with https://github.com/wi2l/fizz