Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tomwei7 committed Aug 22, 2019
1 parent cfd75b8 commit 32c25cf
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/lint/check_go_package_prefix.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lint
import (
"fmt"
"os"
"path/filepath"
"strings"

"github.com/emicklei/proto"
Expand All @@ -24,10 +25,12 @@ func checkFileOptionsGoPackagePrefix(add func(*text.Failure), dirPath string, de
type fileOptionsGoPackagePrefixVisitor struct {
baseAddVisitor

option *proto.Option
option *proto.Option
fileName string
}

func (v *fileOptionsGoPackagePrefixVisitor) OnStart(descriptor *FileDescriptor) error {
v.fileName = descriptor.Filename
v.option = nil
return nil
}
Expand All @@ -40,15 +43,17 @@ func (v *fileOptionsGoPackagePrefixVisitor) VisitOption(element *proto.Option) {

func (v *fileOptionsGoPackagePrefixVisitor) Finally() error {
if v.option == nil {
v.AddFailuref(v.option.Position, `Option "go_package not exists"`)
return nil
}
value := v.option.Constant.Source
prefix := defaultPrefix
if v := os.Getenv("PROTO_GO_PACKAGE_PREFIX"); v != "" {
prefix = v
}
prefix = prefix + "/" + filepath.Dir(v.fileName)
if !strings.HasPrefix(value, prefix) {
v.AddFailuref(v.option.Position, `Option "go_package" must has prefix: %s`, prefix)
v.AddFailuref(v.option.Position, `Expect option "go_package" as: "%s[;package_name]" actual: "%s"`, prefix, value)
}
return nil
}
80 changes: 80 additions & 0 deletions internal/lint/check_message_fields_json_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package lint

import (
"github.com/emicklei/proto"
"github.com/uber/prototool/internal/text"
)

var messageFieldsJSONNameLinter = NewLinter(
"MESSAGE_FIELDS_JSON_NAME",
`Verifies that message field "json_name" option equal "gogoproto.jsontag".`,
checkMessageFieldsJSONName,
)

func checkMessageFieldsJSONName(add func(*text.Failure), dirPath string, descriptors []*FileDescriptor) error {
return runVisitor(messageFieldsJSONNameVisitor{baseAddVisitor: newBaseAddVisitor(add)}, descriptors)
}

type messageFieldsJSONNameVisitor struct {
baseAddVisitor
}

func (v messageFieldsJSONNameVisitor) VisitMessage(message *proto.Message) {
for _, element := range message.Elements {
element.Accept(v)
}
}

func (v messageFieldsJSONNameVisitor) VisitOneof(oneof *proto.Oneof) {
for _, element := range oneof.Elements {
element.Accept(v)
}
}

func (v messageFieldsJSONNameVisitor) VisitNormalField(field *proto.NormalField) {
v.handleField(field.Field)
}

func (v messageFieldsJSONNameVisitor) VisitOneofField(field *proto.OneOfField) {
v.handleField(field.Field)
}

func (v messageFieldsJSONNameVisitor) VisitMapField(field *proto.MapField) {
v.handleField(field.Field)
}

func (v messageFieldsJSONNameVisitor) handleField(field *proto.Field) {
var gogoJSONName, jsonName string
for _, option := range field.Options {
if option.Name == "gogoproto.jsontag" {
gogoJSONName = option.Constant.Source
}
if option.Name == "json_name" {
jsonName = option.Constant.Source
}
}
if gogoJSONName != "" && gogoJSONName != jsonName {
v.AddFailuref(field.Position, `Field %q have different json name gogoproto.jsontag="%s" json_name="%s"`,
field.Name, gogoJSONName, jsonName)
}
}
1 change: 1 addition & 0 deletions internal/lint/lint.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ var (
messageFieldsHaveSentenceCommentsLinter,
messageFieldsNotFloatsLinter,
messageFieldsNoJSONNameLinter,
messageFieldsJSONNameLinter,
messageFieldsTimeLinter,
messageFieldNamesFilenameLinter,
messageFieldNamesFilepathLinter,
Expand Down

0 comments on commit 32c25cf

Please sign in to comment.