Skip to content

Commit

Permalink
fix: golangci-lint
Browse files Browse the repository at this point in the history
  • Loading branch information
EZ4Jam1n committed Jul 26, 2024
1 parent 7e4b78a commit d78cffc
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 71 deletions.
29 changes: 15 additions & 14 deletions protoc-gen-http-swagger/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,15 @@ package generator

import (
"fmt"
"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/protobuf/api"
"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/protobuf/openapi"
"google.golang.org/protobuf/runtime/protoimpl"
"log"
"regexp"
"sort"
"strings"

"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/protobuf/api"
"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/protobuf/openapi"
"google.golang.org/protobuf/runtime/protoimpl"

"google.golang.org/genproto/googleapis/api/annotations"
status_pb "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/protobuf/compiler/protogen"
Expand All @@ -67,8 +68,10 @@ const (
// In order to dynamically add google.rpc.Status responses we need
// to know the message descriptors for google.rpc.Status as well
// as google.protobuf.Any.
var statusProtoDesc = (&status_pb.Status{}).ProtoReflect().Descriptor()
var anyProtoDesc = (&any_pb.Any{}).ProtoReflect().Descriptor()
var (
statusProtoDesc = (&status_pb.Status{}).ProtoReflect().Descriptor()

Check failure on line 72 in protoc-gen-http-swagger/generator/generator.go

View workflow job for this annotation

GitHub Actions / lint module (./protoc-gen-http-swagger)

var `statusProtoDesc` is unused (unused)
anyProtoDesc = (&any_pb.Any{}).ProtoReflect().Descriptor()
)

// OpenAPIGenerator holds internal state needed to generate an OpenAPIv3 document for a transcoded Protocol Buffer service.
type OpenAPIGenerator struct {
Expand Down Expand Up @@ -255,7 +258,6 @@ func (g *OpenAPIGenerator) filterCommentString(c protogen.Comments) string {
}

func (g *OpenAPIGenerator) getSchemaByOption(inputMessage *protogen.Message, bodyType *protoimpl.ExtensionInfo) *openapi.Schema {

// Build an array holding the fields of the message.
definitionProperties := &openapi.Properties{
AdditionalProperties: make([]*openapi.NamedSchemaOrReference, 0),
Expand Down Expand Up @@ -345,7 +347,7 @@ func (g *OpenAPIGenerator) getSchemaByOption(inputMessage *protogen.Message, bod
schema := &openapi.Schema{
Type: "object",
Properties: definitionProperties,
//Required: required,
// Required: required,
}

// Merge any `Schema` annotations with the current
Expand All @@ -367,8 +369,8 @@ func (g *OpenAPIGenerator) buildOperation(
defaultHost string,
path string,
inputMessage *protogen.Message,
outputMessage *protogen.Message) (*openapi.Operation, string) {

outputMessage *protogen.Message,
) (*openapi.Operation, string) {
// Parameters array to hold all parameter objects
var parameters []*openapi.ParameterOrReference

Expand Down Expand Up @@ -403,7 +405,7 @@ func (g *OpenAPIGenerator) buildOperation(
proto.Merge(schema.Schema, extProperty.(*openapi.Schema))
}
}
//按照openapi规范,path参数如果有则一定是required
// 按照openapi规范,path参数如果有则一定是required
required = true
} else if ext = proto.GetExtension(field.Desc.Options(), api.E_Cookie); ext != "" {
paramName = proto.GetExtension(field.Desc.Options(), api.E_Cookie).(string)
Expand Down Expand Up @@ -501,7 +503,7 @@ func (g *OpenAPIGenerator) buildOperation(
RequestBody = &openapi.RequestBodyOrReference{
Oneof: &openapi.RequestBodyOrReference_RequestBody{
RequestBody: &openapi.RequestBody{
//Required: true,
// Required: true,
Content: &openapi.MediaTypes{
AdditionalProperties: additionalProperties,
},
Expand Down Expand Up @@ -568,7 +570,6 @@ func (g *OpenAPIGenerator) buildOperation(
}

func (g *OpenAPIGenerator) getResponseForMessage(d *openapi.Document, message *protogen.Message) (string, *openapi.HeadersOrReferences, *openapi.MediaTypes) {

headers := &openapi.HeadersOrReferences{AdditionalProperties: []*openapi.NamedHeaderOrReference{}}

for _, field := range message.Fields {
Expand All @@ -589,7 +590,7 @@ func (g *OpenAPIGenerator) getResponseForMessage(d *openapi.Document, message *p
}
}

//get api.body、api.raw_body option schema
// get api.body、api.raw_body option schema
bodySchema := g.getSchemaByOption(message, api.E_Body)
rawBodySchema := g.getSchemaByOption(message, api.E_RawBody)

Expand Down Expand Up @@ -641,7 +642,7 @@ func (g *OpenAPIGenerator) getResponseForMessage(d *openapi.Document, message *p
}

// addOperationToDocument adds an operation to the specified path/method.
func (g *OpenAPIGenerator) addOperationToDocument(d *openapi.Document, op *openapi.Operation, path string, methodName string) {
func (g *OpenAPIGenerator) addOperationToDocument(d *openapi.Document, op *openapi.Operation, path, methodName string) {
var selectedPathItem *openapi.NamedPathItem
for _, namedPathItem := range d.Paths.Path {
if namedPathItem.Name == path {
Expand Down
7 changes: 5 additions & 2 deletions protoc-gen-http-swagger/generator/reflector.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
package generator

import (
"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/protobuf/openapi"
"log"
"strings"

"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/protobuf/openapi"

"google.golang.org/protobuf/reflect/protoreflect"

wk "github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/generator/wellknown"
Expand Down Expand Up @@ -172,7 +173,9 @@ func (r *OpenAPIReflector) schemaOrReferenceForMessage(message protoreflect.Mess
ref := r.schemaReferenceForMessage(message)
return &openapi.SchemaOrReference{
Oneof: &openapi.SchemaOrReference_Reference{
Reference: &openapi.Reference{XRef: ref}}}
Reference: &openapi.Reference{XRef: ref},
},
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion protoc-gen-http-swagger/generator/wellknown/mediatypes.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, softwis
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
Expand Down
55 changes: 40 additions & 15 deletions protoc-gen-http-swagger/generator/wellknown/schemas.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, softwis
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
Expand Down Expand Up @@ -39,31 +39,41 @@ import (
func NewStringSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string"}}}
Schema: &v3.Schema{Type: "string"},
},
}
}

func NewBooleanSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "boolean"}}}
Schema: &v3.Schema{Type: "boolean"},
},
}
}

func NewBytesSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string", Format: "bytes"}}}
Schema: &v3.Schema{Type: "string", Format: "bytes"},
},
}
}

func NewIntegerSchema(format string) *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "integer", Format: format}}}
Schema: &v3.Schema{Type: "integer", Format: format},
},
}
}

func NewNumberSchema(format string) *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "number", Format: format}}}
Schema: &v3.Schema{Type: "number", Format: format},
},
}
}

func NewEnumSchema(enum_type *string, field protoreflect.FieldDescriptor) *v3.SchemaOrReference {
Expand All @@ -81,7 +91,9 @@ func NewEnumSchema(enum_type *string, field protoreflect.FieldDescriptor) *v3.Sc
}
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: schema}}
Schema: schema,
},
}
}

func NewListSchema(item_schema *v3.SchemaOrReference) *v3.SchemaOrReference {
Expand All @@ -100,14 +112,18 @@ func NewListSchema(item_schema *v3.SchemaOrReference) *v3.SchemaOrReference {
func NewGoogleApiHttpBodySchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string"}}}
Schema: &v3.Schema{Type: "string"},
},
}
}

// google.protobuf.Timestamp is serialized as a string
func NewGoogleProtobufTimestampSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string", Format: "date-time"}}}
Schema: &v3.Schema{Type: "string", Format: "date-time"},
},
}
}

// google.protobuf.Duration is serialized as a string
Expand Down Expand Up @@ -158,28 +174,36 @@ func NewGoogleProtobufDurationSchema() *v3.SchemaOrReference {
func NewGoogleTypeDateSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string", Format: "date"}}}
Schema: &v3.Schema{Type: "string", Format: "date"},
},
}
}

// google.type.DateTime is serialized as a string
func NewGoogleTypeDateTimeSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string", Format: "date-time"}}}
Schema: &v3.Schema{Type: "string", Format: "date-time"},
},
}
}

// google.protobuf.FieldMask masks is serialized as a string
func NewGoogleProtobufFieldMaskSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "string", Format: "field-mask"}}}
Schema: &v3.Schema{Type: "string", Format: "field-mask"},
},
}
}

// google.protobuf.Struct is equivalent to a JSON object
func NewGoogleProtobufStructSchema() *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "object"}}}
Schema: &v3.Schema{Type: "object"},
},
}
}

// google.protobuf.Value is handled specially
Expand Down Expand Up @@ -242,7 +266,7 @@ func NewGoogleProtobufAnySchema(name string) *v3.NamedSchemaOrReference {
}

// google.rpc.Status is handled specially
func NewGoogleRpcStatusSchema(name string, any_name string) *v3.NamedSchemaOrReference {
func NewGoogleRpcStatusSchema(name, any_name string) *v3.NamedSchemaOrReference {
return &v3.NamedSchemaOrReference{
Name: name,
Value: &v3.SchemaOrReference{
Expand Down Expand Up @@ -308,7 +332,8 @@ func NewGoogleRpcStatusSchema(name string, any_name string) *v3.NamedSchemaOrRef
func NewGoogleProtobufMapFieldEntrySchema(value_field_schema *v3.SchemaOrReference) *v3.SchemaOrReference {
return &v3.SchemaOrReference{
Oneof: &v3.SchemaOrReference_Schema{
Schema: &v3.Schema{Type: "object",
Schema: &v3.Schema{
Type: "object",
AdditionalProperties: &v3.AdditionalPropertiesItem{
Oneof: &v3.AdditionalPropertiesItem_SchemaOrReference{
SchemaOrReference: value_field_schema,
Expand Down
5 changes: 3 additions & 2 deletions protoc-gen-http-swagger/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@ package main

import (
"flag"
"path/filepath"
"strings"

"github.com/hertz-contrib/swagger-generate/protoc-gen-http-swagger/generator"
"google.golang.org/protobuf/compiler/protogen"
"google.golang.org/protobuf/types/pluginpb"
"path/filepath"
"strings"
)

var flags flag.FlagSet
Expand Down
20 changes: 9 additions & 11 deletions protoc-gen-http-swagger/protobuf/api/httpOption.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,15 @@ import (
"google.golang.org/protobuf/runtime/protoimpl"
)

var (
HttpMethodOptions = map[*protoimpl.ExtensionInfo]string{
E_Get: "GET",
E_Post: "POST",
E_Put: "PUT",
E_Patch: "PATCH",
E_Delete: "DELETE",
E_Options: "OPTIONS",
E_Head: "HEAD",
}
)
var HttpMethodOptions = map[*protoimpl.ExtensionInfo]string{
E_Get: "GET",
E_Post: "POST",
E_Put: "PUT",
E_Patch: "PATCH",
E_Delete: "DELETE",
E_Options: "OPTIONS",
E_Head: "HEAD",
}

func GetAllOptions(extensions map[*protoimpl.ExtensionInfo]string, opts ...protoreflect.ProtoMessage) map[string]interface{} {
out := map[string]interface{}{}
Expand Down
3 changes: 2 additions & 1 deletion protoc-gen-http-swagger/protobuf/openapi/annotations.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit d78cffc

Please sign in to comment.