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
Provenance
- Commit
d510595- Tree
394a61f- Author
田欧 <thinkerou@gmail.com>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
3f27866
21 files changed+97 added−16 removed
modified:examples/basic/main.go+3 added-3 removed
4 unmodified lines5 6 "github.com/gin-gonic/gin"7 )8 9-var DB = make(map[string]string)9+var db = make(map[string]string)10 11 func setupRouter() *gin.Engine {12 // Disable Console Color13 // gin.DisableConsoleColor()6 unmodified lines20 21 // 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 lines49 Value string `json:"value" binding:"required"`50 }51 52 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 linesmodified:examples/custom-validation/server.go+1 added
8 unmodified lines9 "github.com/gin-gonic/gin/binding"10 "gopkg.in/go-playground/validator.v8"11 )12 13+// Booking contains binded and validated data.14 type 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 linesmodified:examples/realtime-advanced/main.go+3 added
11 unmodified lines12 StartWorkers()13 StartGin()14 }15 16+// ConfigRuntime sets the number of operating system threads.17 func ConfigRuntime() {18 nuCPU := runtime.NumCPU()19 runtime.GOMAXPROCS(nuCPU)20 fmt.Printf("Running with %d CPUs\n", nuCPU)21 }22 23+// StartWrokers start starsWorker by goroutine.24 func StartWorkers() {25 go statsWorker()26 }27 28+// StartGin starts gin web server with setting router.29 func StartGin() {30 gin.SetMode(gin.ReleaseMode)31 32 router := gin.New()10 unmodified linesmodified:examples/realtime-advanced/stats.go+1 added
48 unmodified lines49 }50 return uint64(connected)51 }52 53+// Stats returns savedStats data.54 func Stats() map[string]uint64 {55 mutexStats.RLock()56 defer mutexStats.RUnlock()57 2 unmodified linesmodified:ginS/gins.go+8 added-1 removed
20 unmodified lines21 })22 return internalEngine23 }24 25+// LoadHTMLGlob is a wrapper for Engine.LoadHTMLGlob.26 func LoadHTMLGlob(pattern string) {27 engine().LoadHTMLGlob(pattern)28 }29 30+// LoadHTMLFiles is a wrapper for Engine.LoadHTMLFiles.31 func LoadHTMLFiles(files ...string) {32 engine().LoadHTMLFiles(files...)33 }34 35+// SetHTMLTemplate is a wrapper for Engine.SetHTMLTemplate.36 func SetHTMLTemplate(templ *template.Template) {37 engine().SetHTMLTemplate(templ)38 }39 1 unmodified line41 func NoRoute(handlers ...gin.HandlerFunc) {42 engine().NoRoute(handlers...)43 }44 42-// NoMethod sets the handlers called when... TODO45+// NoMethod is a wrapper for Engine.NoMethod.46 func NoMethod(handlers ...gin.HandlerFunc) {47 engine().NoMethod(handlers...)48 }49 2 unmodified lines52 func Group(relativePath string, handlers ...gin.HandlerFunc) *gin.RouterGroup {53 return engine().Group(relativePath, handlers...)54 }55 56+// Handle is a wrapper for Engine.Handle.57 func Handle(httpMethod, relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {58 return engine().Handle(httpMethod, relativePath, handlers...)59 }60 31 unmodified lines92 func HEAD(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {93 return engine().HEAD(relativePath, handlers...)94 }95 96+// Any is a wrapper for Engine.Any.97 func Any(relativePath string, handlers ...gin.HandlerFunc) gin.IRoutes {98 return engine().Any(relativePath, handlers...)99 }100 101+// StaticFile is a wrapper for Engine.StaticFile.102 func StaticFile(relativePath, filepath string) gin.IRoutes {103 return engine().StaticFile(relativePath, filepath)104 }105 6 unmodified lines112 func Static(relativePath, root string) gin.IRoutes {113 return engine().Static(relativePath, root)114 }115 116+// StaticFS is a wrapper for Engine.StaticFS.117 func StaticFS(relativePath string, fs http.FileSystem) gin.IRoutes {118 return engine().StaticFS(relativePath, fs)119 }120 27 unmodified linesmodified:internal/json/json.go+7 added-3 removed
7 unmodified lines8 9 import "encoding/json"10 11 var (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 lines8 9 import "github.com/json-iterator/go"10 11 var (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 lines5 package render6 7 import "net/http"8 9+// Data contains ContentType and bytes data.10 type Data struct {11 ContentType string12 Data []byte13 }4 unmodified lines18 _, err = w.Write(r.Data)19 return20 }21 22+// WriteContentType (Data) writes custom ContentType.23 func (r Data) WriteContentType(w http.ResponseWriter) {24 writeContentType(w, []string{r.ContentType})25 }modified:render/html.go+13 added-1 removed
7 unmodified lines8 "html/template"9 "net/http"10 )11 12+// Delims represents a set of Left and Right delimiters for HTML template rendering.13 type Delims struct {14+ // Left delimiter, defaults to {{.13- Left string15+ Left string16+ // Right delimiter, defaults to }}.17 Right string18 }19 20+// HTMLRender interface is to be implemented by HTMLProduction and HTMLDebug.21 type HTMLRender interface {22+ // Instance returns an HTML instance.23 Instance(string, interface{}) Render24 }25 26+// HTMLProduction contains template reference and its delims.27 type HTMLProduction struct {28 Template *template.Template29 Delims Delims30 }31 32+// HTMLDebug contains template delims and pattern and function with file list.33 type HTMLDebug struct {34 Files []string35 Glob string36 Delims Delims37 FuncMap template.FuncMap38 }39 40+// HTML contains template reference and its name with given interface object.41 type HTML struct {42 Template *template.Template43 Name string44 Data interface{}45 }46 47 var htmlContentType = []string{"text/html; charset=utf-8"}48 49+// Instance (HTMLProduction) returns an HTML instance which it realizes Render interface..50 func (r HTMLProduction) Instance(name string, data interface{}) Render {51 return HTML{52 Template: r.Template,53 Name: name,54 Data: data,55 }56 }57 58+// Instance (HTMLDebug) returns an HTML instance which it realizes Render interface..59 func (r HTMLDebug) Instance(name string, data interface{}) Render {60 return HTML{61 Template: r.loadTemplate(),62 Name: name,12 unmodified lines75 }76 panic("the HTML debug render was created without files or glob pattern")77 }78 79+// Render (HTML) executes template and writes its result with custom ContentType for response.80 func (r HTML) Render(w http.ResponseWriter) error {81 r.WriteContentType(w)82 83 if r.Name == "" {1 unmodified line85 }86 return r.Template.ExecuteTemplate(w, r.Name, r.Data)87 }88 89+// WriteContentType (HTML) writes HTML ContentType.90 func (r HTML) WriteContentType(w http.ResponseWriter) {91 writeContentType(w, htmlContentType)92 }modified:render/json_17.go+3 added
10 unmodified lines11 12 "github.com/gin-gonic/gin/internal/json"13 )14 15+// PureJSON contains the given interface object.16 type PureJSON struct {17 Data interface{}18 }19 20+// Render (PureJSON) writes custom ContentType and encodes the given interface object.21 func (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 }27 28+// WriteContentType (PureJSON) writes custom ContentType.29 func (r PureJSON) WriteContentType(w http.ResponseWriter) {30 writeContentType(w, jsonContentType)31 }modified:render/json.go+17 added
11 unmodified lines12 13 "github.com/gin-gonic/gin/internal/json"14 )15 16+// JSON contains the given interface object.17 type JSON struct {18 Data interface{}19 }20 21+// IndentedJSON contains the given interface object.22 type IndentedJSON struct {23 Data interface{}24 }25 26+// SecureJSON contains the given interface object and its prefix.27 type SecureJSON struct {28 Prefix string29 Data interface{}30 }31 32+// JsonpJSON contains the given interface object its callback.33 type JsonpJSON struct {34 Callback string35 Data interface{}36 }37 38+// AsciiJSON contains the given interface object.39 type AsciiJSON struct {40 Data interface{}41 }42 43+// SecureJSONPrefix is a string which represents SecureJSON prefix.44 type SecureJSONPrefix string45 46 var jsonContentType = []string{"application/json; charset=utf-8"}47 var jsonpContentType = []string{"application/javascript; charset=utf-8"}48 var jsonAsciiContentType = []string{"application/json"}49 50+// Render (JSON) writes data with custom ContentType.51 func (r JSON) Render(w http.ResponseWriter) (err error) {52 if err = WriteJSON(w, r.Data); err != nil {53 panic(err)54 }55 return56 }57 58+// WriteContentType (JSON) writes JSON ContentType.59 func (r JSON) WriteContentType(w http.ResponseWriter) {60 writeContentType(w, jsonContentType)61 }62 63+// WriteJSON marshals the given interface object and writes it with custom ContentType.64 func WriteJSON(w http.ResponseWriter, obj interface{}) error {65 writeContentType(w, jsonContentType)66 jsonBytes, err := json.Marshal(obj)67 if err != nil {2 unmodified lines70 w.Write(jsonBytes)71 return nil72 }73 74+// Render (IndentedJSON) marshals the given interface object and writes it with custom ContentType.75 func (r IndentedJSON) Render(w http.ResponseWriter) error {76 r.WriteContentType(w)77 jsonBytes, err := json.MarshalIndent(r.Data, "", " ")78 if err != nil {2 unmodified lines81 w.Write(jsonBytes)82 return nil83 }84 85+// WriteContentType (IndentedJSON) writes JSON ContentType.86 func (r IndentedJSON) WriteContentType(w http.ResponseWriter) {87 writeContentType(w, jsonContentType)88 }89 90+// Render (SecureJSON) marshals the given interface object and writes it with custom ContentType.91 func (r SecureJSON) Render(w http.ResponseWriter) error {92 r.WriteContentType(w)93 jsonBytes, err := json.Marshal(r.Data)94 if err != nil {6 unmodified lines101 w.Write(jsonBytes)102 return nil103 }104 105+// WriteContentType (SecureJSON) writes JSON ContentType.106 func (r SecureJSON) WriteContentType(w http.ResponseWriter) {107 writeContentType(w, jsonContentType)108 }109 110+// Render (JsonpJSON) marshals the given interface object and writes it and its callback with custom ContentType.111 func (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 lines128 129 return nil130 }131 132+// WriteContentType (JsonpJSON) writes Javascript ContentType.133 func (r JsonpJSON) WriteContentType(w http.ResponseWriter) {134 writeContentType(w, jsonpContentType)135 }136 137+// Render (AsciiJSON) marshals the given interface object and writes it with custom ContentType.138 func (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 lines154 w.Write(buffer.Bytes())155 return nil156 }157 158+// WriteContentType (AsciiJSON) writes JSON ContentType.159 func (r AsciiJSON) WriteContentType(w http.ResponseWriter) {160 writeContentType(w, jsonAsciiContentType)161 }modified:render/msgpack.go+4 added
8 unmodified lines9 10 "github.com/ugorji/go/codec"11 )12 13+// MsgPack contains the given interface object.14 type MsgPack struct {15 Data interface{}16 }17 18 var msgpackContentType = []string{"application/msgpack; charset=utf-8"}19 20+// WriteContentType (MsgPack) writes MsgPack ContentType.21 func (r MsgPack) WriteContentType(w http.ResponseWriter) {22 writeContentType(w, msgpackContentType)23 }24 25+// Render (MsgPack) encodes the given interface object and writes data with custom ContentType.26 func (r MsgPack) Render(w http.ResponseWriter) error {27 return WriteMsgPack(w, r.Data)28 }29 30+// WriteMsgPack writes MsgPack ContentType and encodes the given interface object.31 func 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 linemodified:render/protobuf.go+3 added
8 unmodified lines9 10 "github.com/golang/protobuf/proto"11 )12 13+// ProtoBuf contains the given interface object.14 type ProtoBuf struct {15 Data interface{}16 }17 18 var protobufContentType = []string{"application/x-protobuf"}19 20+// Render (ProtoBuf) marshals the given interface object and writes data with custom ContentType.21 func (r ProtoBuf) Render(w http.ResponseWriter) error {22 r.WriteContentType(w)23 24 bytes, err := proto.Marshal(r.Data.(proto.Message))4 unmodified lines29 w.Write(bytes)30 return nil31 }32 33+// WriteContentType (ProtoBuf) writes ProtoBuf ContentType.34 func (r ProtoBuf) WriteContentType(w http.ResponseWriter) {35 writeContentType(w, protobufContentType)36 }modified:render/reader.go+3 added
8 unmodified lines9 "net/http"10 "strconv"11 )12 13+// Reader contains the IO reader and its length, and custom ContentType and other headers.14 type Reader struct {15 ContentType string16 ContentLength int6417 Reader io.Reader8 unmodified lines26 _, err = io.Copy(w, r.Reader)27 return28 }29 30+// WriteContentType (Reader) writes custom ContentType.31 func (r Reader) WriteContentType(w http.ResponseWriter) {32 writeContentType(w, []string{r.ContentType})33 }34 35+// writeHeaders writes custom Header.36 func (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 linesmodified:render/redirect.go+3 added
7 unmodified lines8 "fmt"9 "net/http"10 )11 12+// Redirect contains the http request reference and redirects status code and location.13 type Redirect struct {14 Code int15 Request *http.Request16 Location string17 }18 19+// Render (Redirect) redirects the http request to new location and writes redirect response.20 func (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 lines26 http.Redirect(w, r.Request, r.Location, r.Code)27 return nil28 }29 30+// WriteContentType (Redirect) don't write any ContentType.31 func (r Redirect) WriteContentType(http.ResponseWriter) {}modified:render/render.go+3 added
4 unmodified lines5 package render6 7 import "net/http"8 9+// Render interface is to be implemented by JSON, XML, HTML, YAML and so on.10 type Render interface {11+ // Render writes data with custom ContentType.12 Render(http.ResponseWriter) error13+ // WriteContentType writes custom ContentType.14 WriteContentType(w http.ResponseWriter)15 }16 17 var (24 unmodified linesmodified:render/text.go+4 added
8 unmodified lines9 "io"10 "net/http"11 )12 13+// String contains the given interface object slice and its format.14 type String struct {15 Format string16 Data []interface{}17 }18 19 var plainContentType = []string{"text/plain; charset=utf-8"}20 21+// Render (String) writes data with custom ContentType.22 func (r String) Render(w http.ResponseWriter) error {23 WriteString(w, r.Format, r.Data)24 return nil25 }26 27+// WriteContentType (String) writes Plain ContentType.28 func (r String) WriteContentType(w http.ResponseWriter) {29 writeContentType(w, plainContentType)30 }31 32+// WriteString writes data according to its format and write custom ContentType.33 func 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 linesmodified:render/xml.go+3 added
7 unmodified lines8 "encoding/xml"9 "net/http"10 )11 12+// XML contains the given interface object.13 type XML struct {14 Data interface{}15 }16 17 var xmlContentType = []string{"application/xml; charset=utf-8"}18 19+// Render (XML) encodes the given interface object and writes data with custom ContentType.20 func (r XML) Render(w http.ResponseWriter) error {21 r.WriteContentType(w)22 return xml.NewEncoder(w).Encode(r.Data)23 }24 25+// WriteContentType (XML) writes XML ContentType for response.26 func (r XML) WriteContentType(w http.ResponseWriter) {27 writeContentType(w, xmlContentType)28 }modified:render/yaml.go+3 added
8 unmodified lines9 10 "gopkg.in/yaml.v2"11 )12 13+// YAML contains the given interface object.14 type YAML struct {15 Data interface{}16 }17 18 var yamlContentType = []string{"application/x-yaml; charset=utf-8"}19 20+// Render (YAML) marshals the given interface object and writes data with custom ContentType.21 func (r YAML) Render(w http.ResponseWriter) error {22 r.WriteContentType(w)23 24 bytes, err := yaml.Marshal(r.Data)4 unmodified lines29 w.Write(bytes)30 return nil31 }32 33+// WriteContentType (YAML) writes YAML ContentType for response.34 func (r YAML) WriteContentType(w http.ResponseWriter) {35 writeContentType(w, yamlContentType)36 }modified:README.md+1 added
655 unmodified lines656 "github.com/gin-gonic/gin/binding"657 "gopkg.in/go-playground/validator.v8"658 )659 660+// Booking contains binded and validated data.661 type 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 linesmodified:utils.go+4 added-4 removed
12 unmodified lines13 "runtime"14 "strings"15 )16 17+// BindKey indicates a default bind key.18 const BindKey = "_gin-gonic/gin/bindkey"19 20+// Bind is a helper function for given interface object and returns a Gin middleware.21 func 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 lines34 }35 }36 }37 36-// 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.39 func WrapF(f http.HandlerFunc) HandlerFunc {40 return func(c *Context) {41 f(c.Writer, c.Request)42 }43 }44 44-// 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.46 func WrapH(h http.Handler) HandlerFunc {47 return func(c *Context) {48 h.ServeHTTP(c.Writer, c.Request)49 }102 unmodified lines