Skip to content
Myr

chore: add some annotations (#1544)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

ref: #1075 because I am not a native English, maybe have a bit problem.

authored by 田欧 · committed by Bo-Yi Wu · parent 3f27866 · Browse files · 21 files+97 added−16 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

21 files+97 added−16 removed

  • other:+97 added−16 removed

21 files+97 added−16 removed

modified:examples/basic/main.go+3 added-3 removed
4 unmodified lines
56 "github.com/gin-gonic/gin"7)89var DB = make(map[string]string)9var db = make(map[string]string)1011func setupRouter() *gin.Engine {12 // Disable Console Color13 // gin.DisableConsoleColor()
6 unmodified lines
2021 // Get user value22 r.GET("/user/:name", func(c *gin.Context) {23 user := c.Params.ByName("name")24 value, ok := DB[user]24 value, ok := db[user]25 if ok {26 c.JSON(http.StatusOK, gin.H{"user": user, "value": value})27 } else {28 c.JSON(http.StatusOK, gin.H{"user": user, "status": "no value"})
20 unmodified lines
49 Value string `json:"value" binding:"required"`50 }5152 if c.Bind(&json) == nil {53 DB[user] = json.Value53 db[user] = json.Value54 c.JSON(http.StatusOK, gin.H{"status": "ok"})55 }56 })57
8 unmodified lines
modified:examples/custom-validation/server.go+1 added
8 unmodified lines
9 "github.com/gin-gonic/gin/binding"10 "gopkg.in/go-playground/validator.v8"11)1213// Booking contains binded and validated data.14type Booking struct {15 CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`16 CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"`17}
33 unmodified lines
modified:examples/realtime-advanced/main.go+3 added
11 unmodified lines
12 StartWorkers()13 StartGin()14}1516// ConfigRuntime sets the number of operating system threads.17func ConfigRuntime() {18 nuCPU := runtime.NumCPU()19 runtime.GOMAXPROCS(nuCPU)20 fmt.Printf("Running with %d CPUs\n", nuCPU)21}2223// StartWrokers start starsWorker by goroutine.24func StartWorkers() {25 go statsWorker()26}2728// StartGin starts gin web server with setting router.29func StartGin() {30 gin.SetMode(gin.ReleaseMode)3132 router := gin.New()
10 unmodified lines
modified:examples/realtime-advanced/stats.go+1 added
48 unmodified lines
49 }50 return uint64(connected)51}5253// Stats returns savedStats data.54func Stats() map[string]uint64 {55 mutexStats.RLock()56 defer mutexStats.RUnlock()57
2 unmodified lines
modified:ginS/gins.go+8 added-1 removed
20 unmodified lines
21 })22 return internalEngine23}2425// LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.26func LoadHTMLGlob(pattern string) {27 engine().LoadHTMLGlob(pattern)28}2930// LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.31func LoadHTMLFiles(files ...string) {32 engine().LoadHTMLFiles(files...)33}3435// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.36func SetHTMLTemplate(templ *template.Template) {37 engine().SetHTMLTemplate(templ)38}39
1 unmodified line
41func NoRoute(handlers ...gin.HandlerFunc) {42 engine().NoRoute(handlers...)43}4442// NoMethod sets the handlers called when... TODO45// NoMethod is a wrapper for Engine.NoMethod.46func NoMethod(handlers ...gin.HandlerFunc) {47 engine().NoMethod(handlers...)48}49
2 unmodified lines
52func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {53 return engine().Group(relativePath, handlers...)54}5556// Handle is a wrapper for Engine.Handle.57func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {58 return engine().Handle(httpMethod, relativePath, handlers...)59}60
31 unmodified lines
92func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {93 return engine().HEAD(relativePath, handlers...)94}9596// Any is a wrapper for Engine.Any.97func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {98 return engine().Any(relativePath, handlers...)99}100101// StaticFile is a wrapper for Engine.StaticFile.102func StaticFile(relativePath, filepath string) gin.IRoutes {103 return engine().StaticFile(relativePath, filepath)104}105
6 unmodified lines
112func Static(relativePath, root string) gin.IRoutes {113 return engine().Static(relativePath, root)114}115116// StaticFS is a wrapper for Engine.StaticFS.117func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {118 return engine().StaticFS(relativePath, fs)119}120
27 unmodified lines
modified:internal/json/json.go+7 added-3 removed
7 unmodified lines
89import "encoding/json"1011var (12 // Marshal is exported by gin/json package.12 Marshal = json.Marshal13 Marshal = json.Marshal14 // MarshalIndent is exported by gin/json package.15 MarshalIndent = json.MarshalIndent14 NewDecoder = json.NewDecoder15 NewEncoder = json.NewEncoder16 // NewDecoder is exported by gin/json package.17 NewDecoder = json.NewDecoder18 // NewEncoder is exported by gin/json package.19 NewEncoder = json.NewEncoder20)
modified:internal/json/jsoniter.go+8 added-4 removed
7 unmodified lines
89import "github.com/json-iterator/go"1011var (12 json = jsoniter.ConfigCompatibleWithStandardLibrary13 Marshal = json.Marshal12 json = jsoniter.ConfigCompatibleWithStandardLibrary13 // Marshal is exported by gin/json package.14 Marshal = json.Marshal15 // MarshalIndent is exported by gin/json package.16 MarshalIndent = json.MarshalIndent15 NewDecoder = json.NewDecoder16 NewEncoder = json.NewEncoder17 // NewDecoder is exported by gin/json package.18 NewDecoder = json.NewDecoder19 // NewEncoder is exported by gin/json package.20 NewEncoder = json.NewEncoder21)
modified:render/data.go+2 added
4 unmodified lines
5package render67import "net/http"89// Data contains ContentType and bytes data.10type Data struct {11 ContentType string12 Data []byte13}
4 unmodified lines
18 _, err = w.Write(r.Data)19 return20}2122// WriteContentType (Data) writes custom ContentType.23func (r Data) WriteContentType(w http.ResponseWriter) {24 writeContentType(w, []string{r.ContentType})25}
modified:render/html.go+13 added-1 removed
7 unmodified lines
8 "html/template"9 "net/http"10)1112// Delims represents a set of Left and Right delimiters for HTML template rendering.13type Delims struct {14 // Left delimiter, defaults to {{.13 Left string15 Left string16 // Right delimiter, defaults to }}.17 Right string18}1920// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.21type HTMLRender interface {22 // Instance returns an HTML instance.23 Instance(string, interface{}) Render24}2526// HTMLProduction contains template reference and its delims.27type HTMLProduction struct {28 Template *template.Template29 Delims Delims30}3132// HTMLDebug contains template delims and pattern and function with file list.33type HTMLDebug struct {34 Files []string35 Glob string36 Delims Delims37 FuncMap template.FuncMap38}3940// HTML contains template reference and its name with given interface object.41type HTML struct {42 Template *template.Template43 Name string44 Data interface{}45}4647var htmlContentType = []string{"text/html; charset=utf-8"}4849// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface..50func (r HTMLProduction) Instance(name string, data interface{}) Render {51 return HTML{52 Template: r.Template,53 Name: name,54 Data: data,55 }56}5758// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface..59func (r HTMLDebug) Instance(name string, data interface{}) Render {60 return HTML{61 Template: r.loadTemplate(),62 Name: name,
12 unmodified lines
75 }76 panic("the HTML debug render was created without files or glob pattern")77}7879// Render (HTML) executes template and writes its result with custom ContentType for response.80func (r HTML) Render(w http.ResponseWriter) error {81 r.WriteContentType(w)8283 if r.Name == "" {
1 unmodified line
85 }86 return r.Template.ExecuteTemplate(w, r.Name, r.Data)87}8889// WriteContentType (HTML) writes HTML ContentType.90func (r HTML) WriteContentType(w http.ResponseWriter) {91 writeContentType(w, htmlContentType)92}
modified:render/json_17.go+3 added
10 unmodified lines
1112 "github.com/gin-gonic/gin/internal/json"13)1415// PureJSON contains the given interface object.16type PureJSON struct {17 Data interface{}18}1920// Render (PureJSON) writes custom ContentType and encodes the given interface object.21func (r PureJSON) Render(w http.ResponseWriter) error {22 r.WriteContentType(w)23 encoder := json.NewEncoder(w)24 encoder.SetEscapeHTML(false)25 return encoder.Encode(r.Data)26}2728// WriteContentType (PureJSON) writes custom ContentType.29func (r PureJSON) WriteContentType(w http.ResponseWriter) {30 writeContentType(w, jsonContentType)31}
modified:render/json.go+17 added
11 unmodified lines
1213 "github.com/gin-gonic/gin/internal/json"14)1516// JSON contains the given interface object.17type JSON struct {18 Data interface{}19}2021// IndentedJSON contains the given interface object.22type IndentedJSON struct {23 Data interface{}24}2526// SecureJSON contains the given interface object and its prefix.27type SecureJSON struct {28 Prefix string29 Data interface{}30}3132// JsonpJSON contains the given interface object its callback.33type JsonpJSON struct {34 Callback string35 Data interface{}36}3738// AsciiJSON contains the given interface object.39type AsciiJSON struct {40 Data interface{}41}4243// SecureJSONPrefix is a string which represents SecureJSON prefix.44type SecureJSONPrefix string4546var jsonContentType = []string{"application/json; charset=utf-8"}47var jsonpContentType = []string{"application/javascript; charset=utf-8"}48var jsonAsciiContentType = []string{"application/json"}4950// Render (JSON) writes data with custom ContentType.51func (r JSON) Render(w http.ResponseWriter) (err error) {52 if err = WriteJSON(w, r.Data); err != nil {53 panic(err)54 }55 return56}5758// WriteContentType (JSON) writes JSON ContentType.59func (r JSON) WriteContentType(w http.ResponseWriter) {60 writeContentType(w, jsonContentType)61}6263// WriteJSON marshals the given interface object and writes it with custom ContentType.64func WriteJSON(w http.ResponseWriter, obj interface{}) error {65 writeContentType(w, jsonContentType)66 jsonBytes, err := json.Marshal(obj)67 if err != nil {
2 unmodified lines
70 w.Write(jsonBytes)71 return nil72}7374// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.75func (r IndentedJSON) Render(w http.ResponseWriter) error {76 r.WriteContentType(w)77 jsonBytes, err := json.MarshalIndent(r.Data, "", " ")78 if err != nil {
2 unmodified lines
81 w.Write(jsonBytes)82 return nil83}8485// WriteContentType (IndentedJSON) writes JSON ContentType.86func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {87 writeContentType(w, jsonContentType)88}8990// Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.91func (r SecureJSON) Render(w http.ResponseWriter) error {92 r.WriteContentType(w)93 jsonBytes, err := json.Marshal(r.Data)94 if err != nil {
6 unmodified lines
101 w.Write(jsonBytes)102 return nil103}104105// WriteContentType (SecureJSON) writes JSON ContentType.106func (r SecureJSON) WriteContentType(w http.ResponseWriter) {107 writeContentType(w, jsonContentType)108}109110// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.111func (r JsonpJSON) Render(w http.ResponseWriter) (err error) {112 r.WriteContentType(w)113 ret, err := json.Marshal(r.Data)114 if err != nil {
13 unmodified lines
128129 return nil130}131132// WriteContentType (JsonpJSON) writes Javascript ContentType.133func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {134 writeContentType(w, jsonpContentType)135}136137// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.138func (r AsciiJSON) Render(w http.ResponseWriter) (err error) {139 r.WriteContentType(w)140 ret, err := json.Marshal(r.Data)141 if err != nil {
12 unmodified lines
154 w.Write(buffer.Bytes())155 return nil156}157158// WriteContentType (AsciiJSON) writes JSON ContentType.159func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {160 writeContentType(w, jsonAsciiContentType)161}
modified:render/msgpack.go+4 added
8 unmodified lines
910 "github.com/ugorji/go/codec"11)1213// MsgPack contains the given interface object.14type MsgPack struct {15 Data interface{}16}1718var msgpackContentType = []string{"application/msgpack; charset=utf-8"}1920// WriteContentType (MsgPack) writes MsgPack ContentType.21func (r MsgPack) WriteContentType(w http.ResponseWriter) {22 writeContentType(w, msgpackContentType)23}2425// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.26func (r MsgPack) Render(w http.ResponseWriter) error {27 return WriteMsgPack(w, r.Data)28}2930// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.31func WriteMsgPack(w http.ResponseWriter, obj interface{}) error {32 writeContentType(w, msgpackContentType)33 var mh codec.MsgpackHandle34 return codec.NewEncoder(w, &mh).Encode(obj)
1 unmodified line
modified:render/protobuf.go+3 added
8 unmodified lines
910 "github.com/golang/protobuf/proto"11)1213// ProtoBuf contains the given interface object.14type ProtoBuf struct {15 Data interface{}16}1718var protobufContentType = []string{"application/x-protobuf"}1920// Render (ProtoBuf) marshals the given interface object and writes data with custom ContentType.21func (r ProtoBuf) Render(w http.ResponseWriter) error {22 r.WriteContentType(w)2324 bytes, err := proto.Marshal(r.Data.(proto.Message))
4 unmodified lines
29 w.Write(bytes)30 return nil31}3233// WriteContentType (ProtoBuf) writes ProtoBuf ContentType.34func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {35 writeContentType(w, protobufContentType)36}
modified:render/reader.go+3 added
8 unmodified lines
9 "net/http"10 "strconv"11)1213// Reader contains the IO reader and its length, and custom ContentType and other headers.14type Reader struct {15 ContentType string16 ContentLength int6417 Reader io.Reader
8 unmodified lines
26 _, err = io.Copy(w, r.Reader)27 return28}2930// WriteContentType (Reader) writes custom ContentType.31func (r Reader) WriteContentType(w http.ResponseWriter) {32 writeContentType(w, []string{r.ContentType})33}3435// writeHeaders writes custom Header.36func (r Reader) writeHeaders(w http.ResponseWriter, headers map[string]string) {37 header := w.Header()38 for k, v := range headers {39 if val := header[k]; len(val) == 0 {
4 unmodified lines
modified:render/redirect.go+3 added
7 unmodified lines
8 "fmt"9 "net/http"10)1112// Redirect contains the http request reference and redirects status code and location.13type Redirect struct {14 Code int15 Request *http.Request16 Location string17}1819// Render (Redirect) redirects the http request to new location and writes redirect response.20func (r Redirect) Render(w http.ResponseWriter) error {21 // todo(thinkerou): go1.6 not support StatusPermanentRedirect(308)22 // when we upgrade go version we can use http.StatusPermanentRedirect23 if (r.Code < 300 || r.Code > 308) && r.Code != 201 {
2 unmodified lines
26 http.Redirect(w, r.Request, r.Location, r.Code)27 return nil28}2930// WriteContentType (Redirect) don't write any ContentType.31func (r Redirect) WriteContentType(http.ResponseWriter) {}
modified:render/render.go+3 added
4 unmodified lines
5package render67import "net/http"89// Render interface is to be implemented by JSON, XML, HTML, YAML and so on.10type Render interface {11 // Render writes data with custom ContentType.12 Render(http.ResponseWriter) error13 // WriteContentType writes custom ContentType.14 WriteContentType(w http.ResponseWriter)15}1617var (
24 unmodified lines
modified:render/text.go+4 added
8 unmodified lines
9 "io"10 "net/http"11)1213// String contains the given interface object slice and its format.14type String struct {15 Format string16 Data []interface{}17}1819var plainContentType = []string{"text/plain; charset=utf-8"}2021// Render (String) writes data with custom ContentType.22func (r String) Render(w http.ResponseWriter) error {23 WriteString(w, r.Format, r.Data)24 return nil25}2627// WriteContentType (String) writes Plain ContentType.28func (r String) WriteContentType(w http.ResponseWriter) {29 writeContentType(w, plainContentType)30}3132// WriteString writes data according to its format and write custom ContentType.33func WriteString(w http.ResponseWriter, format string, data []interface{}) {34 writeContentType(w, plainContentType)35 if len(data) > 0 {36 fmt.Fprintf(w, format, data...)
4 unmodified lines
modified:render/xml.go+3 added
7 unmodified lines
8 "encoding/xml"9 "net/http"10)1112// XML contains the given interface object.13type XML struct {14 Data interface{}15}1617var xmlContentType = []string{"application/xml; charset=utf-8"}1819// Render (XML) encodes the given interface object and writes data with custom ContentType.20func (r XML) Render(w http.ResponseWriter) error {21 r.WriteContentType(w)22 return xml.NewEncoder(w).Encode(r.Data)23}2425// WriteContentType (XML) writes XML ContentType for response.26func (r XML) WriteContentType(w http.ResponseWriter) {27 writeContentType(w, xmlContentType)28}
modified:render/yaml.go+3 added
8 unmodified lines
910 "gopkg.in/yaml.v2"11)1213// YAML contains the given interface object.14type YAML struct {15 Data interface{}16}1718var yamlContentType = []string{"application/x-yaml; charset=utf-8"}1920// Render (YAML) marshals the given interface object and writes data with custom ContentType.21func (r YAML) Render(w http.ResponseWriter) error {22 r.WriteContentType(w)2324 bytes, err := yaml.Marshal(r.Data)
4 unmodified lines
29 w.Write(bytes)30 return nil31}3233// WriteContentType (YAML) writes YAML ContentType for response.34func (r YAML) WriteContentType(w http.ResponseWriter) {35 writeContentType(w, yamlContentType)36}
modified:README.md+1 added
655 unmodified lines
656 "github.com/gin-gonic/gin/binding"657 "gopkg.in/go-playground/validator.v8"658)659660// Booking contains binded and validated data.661type Booking struct {662 CheckIn time.Time `form:"check_in" binding:"required,bookabledate" time_format:"2006-01-02"`663 CheckOut time.Time `form:"check_out" binding:"required,gtfield=CheckIn" time_format:"2006-01-02"`664}
1227 unmodified lines
modified:utils.go+4 added-4 removed
12 unmodified lines
13 "runtime"14 "strings"15)1617// BindKey indicates a default bind key.18const BindKey = "_gin-gonic/gin/bindkey"1920// Bind is a helper function for given interface object and returns a Gin middleware.21func Bind(val interface{}) HandlerFunc {22 value := reflect.ValueOf(val)23 if value.Kind() == reflect.Ptr {24 panic(`Bind struct can not be a pointer. Example:
9 unmodified lines
34 }35 }36}3736// WrapF is a helper function for wrapping http.HandlerFunc37// Returns a Gin middleware38// WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware.39func WrapF(f http.HandlerFunc) HandlerFunc {40 return func(c *Context) {41 f(c.Writer, c.Request)42 }43}4444// WrapH is a helper function for wrapping http.Handler45// Returns a Gin middleware45// WrapH is a helper function for wrapping http.Handler and returns a Gin middleware.46func WrapH(h http.Handler) HandlerFunc {47 return func(c *Context) {48 h.ServeHTTP(c.Writer, c.Request)49 }
102 unmodified lines