diff --git a/go.mod b/go.mod index 9fe8c5e..5f51cba 100644 --- a/go.mod +++ b/go.mod @@ -7,7 +7,7 @@ require ( github.com/getkin/kin-openapi v0.123.0 github.com/goccy/go-json v0.10.0 github.com/invopop/yaml v0.2.0 - github.com/pubgo/funk v0.5.30 + github.com/pubgo/funk v0.5.41 github.com/stretchr/testify v1.8.4 k8s.io/kube-openapi v0.0.0-20221123214604-86e75ddd809a ) @@ -17,7 +17,7 @@ require ( github.com/davecgh/go-spew v1.1.1 // indirect github.com/go-openapi/jsonpointer v0.20.2 // indirect github.com/go-openapi/swag v0.22.8 // indirect - github.com/golang/protobuf v1.5.2 // indirect + github.com/golang/protobuf v1.5.4 // indirect github.com/josharian/intern v1.0.0 // indirect github.com/k0kubun/pp/v3 v3.2.0 // indirect github.com/mailru/easyjson v0.7.7 // indirect @@ -29,8 +29,10 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rs/zerolog v1.29.0 // indirect golang.org/x/exp v0.0.0-20221114191408-850992195362 // indirect - golang.org/x/sys v0.4.0 // indirect - golang.org/x/text v0.5.0 // indirect - google.golang.org/protobuf v1.28.1 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c // indirect + google.golang.org/grpc v1.51.0 // indirect + google.golang.org/protobuf v1.33.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index 258b81b..15e6f52 100644 --- a/go.sum +++ b/go.sum @@ -15,10 +15,8 @@ github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= github.com/goccy/go-json v0.10.0 h1:mXKd9Qw4NuzShiRlOXKews24ufknHO7gx30lsDyokKA= github.com/goccy/go-json v0.10.0/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk= -github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw= -github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY= -github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= +github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek= +github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps= github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY= github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q= @@ -46,8 +44,8 @@ github.com/phuslu/goid v1.0.0/go.mod h1:txc2fUIdrdnn+v9Vq+QpiPQ3dnrXEchjoVDgic+r github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/pubgo/funk v0.5.30 h1:uo5RblUBR9Opl9j0yzbQoACXNoegtrcvydfLQIdWgu4= -github.com/pubgo/funk v0.5.30/go.mod h1:Z5Wp7OoxjmSlWH+6waFSbao5MSD942LVhkBSlUu4s4s= +github.com/pubgo/funk v0.5.41 h1:vcDD6SWsz6g9CoD/whMIDgcfnGBfvCoZa6OTJ9iB9Tk= +github.com/pubgo/funk v0.5.41/go.mod h1:gKCw72+MK7xPiUGY1Z/bdGJMrSfVi87r0x/7d1GtKU4= github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY= github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= @@ -58,21 +56,20 @@ github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXl github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= golang.org/x/exp v0.0.0-20221114191408-850992195362 h1:NoHlPRbyl1VFI6FjwHtPQCN7wAMXI6cKcqrmXhOOfBQ= golang.org/x/exp v0.0.0-20221114191408-850992195362/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc= -golang.org/x/net v0.4.0 h1:Q5QPcMlvfxFTAPV0+07Xz/MpK9NTXu2VDUuy0FeMfaU= +golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4= golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.4.0 h1:Zr2JFtRQNX3BCZ8YtxRE9hNJYC8J6I1MVbMg6owUp18= -golang.org/x/sys v0.4.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/text v0.5.0 h1:OLmvp0KP+FVG99Ct/qFiL/Fhk4zp4QQnZ7b2U+5piUM= -golang.org/x/text v0.5.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c h1:QgY/XxIAIeccR+Ca/rDdKubLIU9rcJ3xfy1DC/Wd2Oo= +google.golang.org/genproto v0.0.0-20221027153422-115e99e71e1c/go.mod h1:CGI5F/G+E5bKwmfYo09AXuVN4dD894kIKUFmVbP2/Fo= google.golang.org/grpc v1.51.0 h1:E1eGv1FTqoLIdnBCZufiSHgKjlqG6fKFf6pPWtMTh8U= -google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= -google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.28.1 h1:d0NfwRgPtno5B1Wa6L2DAG+KivqkdutMf1UhdNx175w= -google.golang.org/protobuf v1.28.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= +google.golang.org/grpc v1.51.0/go.mod h1:wgNDFcnuBGmxLKI/qn4T+m5BtEBYXJPvibbUPsAIPww= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/internal/examples/main.go b/internal/examples/main.go index a71b1c6..1f04162 100644 --- a/internal/examples/main.go +++ b/internal/examples/main.go @@ -1,11 +1,13 @@ package main import ( + "fmt" + "net/http" + "github.com/pubgo/funk/assert" "github.com/pubgo/funk/recovery" "github.com/pubgo/opendoc/opendoc" "github.com/pubgo/opendoc/security" - "net/http" ) type TestQueryReqAAA struct { @@ -19,7 +21,7 @@ type TestQueryReqAAA struct { func main() { defer recovery.Exit() - var doc = opendoc.New(func(swag *opendoc.Swagger) { + doc := opendoc.New(func(swag *opendoc.Swagger) { swag.Config.Title = "this service web title " swag.Description = "this is description" swag.License = &opendoc.License{ @@ -63,10 +65,11 @@ func main() { }) }) - //data := assert.Must1(doc.MarshalYAML()) - //assert.Exit(os.WriteFile("openapi.yaml", data, 0644)) + // data := assert.Must1(doc.MarshalYAML()) + // assert.Exit(os.WriteFile("openapi.yaml", data, 0644)) - var app = http.NewServeMux() + app := http.NewServeMux() doc.InitRouter(app) + fmt.Println("http://localhost:8080/debug/apidocs") assert.Exit(http.ListenAndServe("localhost:8080", app)) } diff --git a/opendoc/aaa.go b/opendoc/aaa.go index f0d1c1d..97577df 100644 --- a/opendoc/aaa.go +++ b/opendoc/aaa.go @@ -4,10 +4,12 @@ import ( "github.com/getkin/kin-openapi/openapi3" ) -type License = openapi3.License -type Contact = openapi3.Contact -type Servers = openapi3.Servers -type Server = openapi3.Server +type ( + License = openapi3.License + Contact = openapi3.Contact + Servers = openapi3.Servers + Server = openapi3.Server +) // NamedEnum returns the enumerated acceptable values with according string names. type NamedEnum interface { diff --git a/opendoc/operation.go b/opendoc/operation.go index cc97674..2cba5e9 100644 --- a/opendoc/operation.go +++ b/opendoc/operation.go @@ -87,7 +87,7 @@ func (op *Operation) SetOperation(operationID string) *Operation { return op } -func (op *Operation) SetModel(req interface{}, rsp interface{}) *Operation { +func (op *Operation) SetModel(req, rsp interface{}) *Operation { checkModelType(req) op.request = req diff --git a/opendoc/service.go b/opendoc/service.go index 93cc626..35bd71d 100644 --- a/opendoc/service.go +++ b/opendoc/service.go @@ -97,7 +97,7 @@ func (s *Service) PostOf(cb func(op *Operation)) *Service { } func (s *Service) Openapi() map[string]*openapi3.PathItem { - var routes = make(map[string]*openapi3.PathItem) + routes := make(map[string]*openapi3.PathItem) for i := range s.operations { op := s.operations[i] if routes[op.path] == nil { diff --git a/opendoc/swagger.go b/opendoc/swagger.go index 5b61d83..2fc71cb 100644 --- a/opendoc/swagger.go +++ b/opendoc/swagger.go @@ -32,14 +32,14 @@ func (s *Swagger) SetRootPath(path string) { } func (s *Swagger) ServiceOf(name string, cb func(srv *Service)) { - var srv = newService(name) + srv := newService(name) srv.prefix = s.rootPath s.Routers = append(s.Routers, srv) cb(srv) } func (s *Swagger) WithService() *Service { - var srv = new(Service) + srv := new(Service) srv.prefix = s.rootPath s.Routers = append(s.Routers, srv) return srv @@ -50,7 +50,7 @@ func (s *Swagger) buildSwagger() *openapi3.T { s.Config = DefaultCfg() } - var t = &openapi3.T{ + t := &openapi3.T{ OpenAPI: "3.0.0", Servers: s.Servers, Components: &components, @@ -87,7 +87,7 @@ func (s *Swagger) InitRouter(r *http.ServeMux) { } func (s *Swagger) OpenapiDataHandler() http.HandlerFunc { - var bytes = assert.Must1(s.MarshalYAML()) + bytes := assert.Must1(s.MarshalYAML()) return func(writer http.ResponseWriter, request *http.Request) { assert.Must1(writer.Write(bytes)) } diff --git a/opendoc/swagger_test.go b/opendoc/swagger_test.go index 89bd9f2..71e8021 100644 --- a/opendoc/swagger_test.go +++ b/opendoc/swagger_test.go @@ -34,18 +34,18 @@ type testQueryReq struct { } func TestGenSchema(t *testing.T) { - var ref, s = genSchema(testQueryReq{}) + ref, s := genSchema(testQueryReq{}) assert.NotNil(t, s) assert.Equal(t, "#/components/schemas/com.github.pubgo.opendoc.testQueryReq", ref) - var data, err = json.Marshal(s) + data, err := json.Marshal(s) assert.NoError(t, err) assert.Equal(t, `{"properties":{"name":{"default":"test","description":"name of model","nullable":true,"type":"string"},"rsp":{"$ref":"#/components/schemas/com.github.pubgo.opendoc.testQueryRsp"}},"required":["name","rsp"],"type":"object"}`, string(data), ) - var p = genParameters(testQueryReq{}) + p := genParameters(testQueryReq{}) data, err = json.Marshal(p) assert.NoError(t, err) assert.Equal(t, diff --git a/opendoc/util.go b/opendoc/util.go index ec45577..876a8cc 100644 --- a/opendoc/util.go +++ b/opendoc/util.go @@ -20,7 +20,7 @@ import ( ) func getTag(tags *structtag.Tags, key string, fn func(tag *structtag.Tag)) { - var tag, err = tags.Get(key) + tag, err := tags.Get(key) if err == nil && tag.Key != "" { fn(tag) } @@ -249,8 +249,8 @@ func genResponses(response interface{}, contentType ...string) *openapi3.Respons _, schema := genSchema(response) content := openapi3.NewContentWithSchema(schema, contentType) - var docText = http.StatusText(http.StatusOK) - var rsp = &openapi3.ResponseRef{ + docText := http.StatusText(http.StatusOK) + rsp := &openapi3.ResponseRef{ Value: &openapi3.Response{ Description: &docText, Content: content, @@ -264,7 +264,7 @@ func genResponses(response interface{}, contentType ...string) *openapi3.Respons } func isParameter(val *structtag.Tags) bool { - var params = []string{queryTag, uriTag, pathTag, headerTag, cookieTag} + params := []string{queryTag, uriTag, pathTag, headerTag, cookieTag} for i := range params { if _, err := val.Get(params[i]); err == nil { return true diff --git a/security/basic.go b/security/basic.go index e9e527f..7501066 100644 --- a/security/basic.go +++ b/security/basic.go @@ -4,8 +4,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" ) -type Basic struct { -} +type Basic struct{} type User struct { Username string diff --git a/security/bearer.go b/security/bearer.go index 52773bb..ae8f9e2 100644 --- a/security/bearer.go +++ b/security/bearer.go @@ -4,8 +4,7 @@ import ( "github.com/getkin/kin-openapi/openapi3" ) -type Bearer struct { -} +type Bearer struct{} func (b Bearer) Provider() string { return "Bearer" diff --git a/templates/template.go b/templates/template.go index 79d4fe5..444a97c 100644 --- a/templates/template.go +++ b/templates/template.go @@ -17,9 +17,11 @@ var swaggerFile string //go:embed rapidoc.html var rApiDocFile string -var reDocTemplate = assert.Exit1(template.New("").Parse(reDocFile)) -var swaggerTemplate = assert.Exit1(template.New("").Parse(swaggerFile)) -var rApiDocFileTemplate = assert.Exit1(template.New("").Parse(rApiDocFile)) +var ( + reDocTemplate = assert.Exit1(template.New("").Parse(reDocFile)) + swaggerTemplate = assert.Exit1(template.New("").Parse(swaggerFile)) + rApiDocFileTemplate = assert.Exit1(template.New("").Parse(rApiDocFile)) +) func RApiDocHandler(url string) http.HandlerFunc { return func(writer http.ResponseWriter, request *http.Request) {