Esempio n. 1
0
func fillTreeWithFile(tree *tree, file *descriptor.FileDescriptorProto) {
	key := fmt.Sprintf(".%s", file.GetPackage())
	locs := make(map[string]*descriptor.SourceCodeInfo_Location)
	for _, loc := range file.GetSourceCodeInfo().GetLocation() {
		if loc.LeadingComments == nil {
			continue
		}
		var p []string
		for _, n := range loc.Path {
			p = append(p, strconv.Itoa(int(n)))
		}
		locs[strings.Join(p, ",")] = loc
	}

	// Messages
	for idx, proto := range file.GetMessageType() {
		fillTreeWithMessage(tree, key, proto, fmt.Sprintf("4,%d", idx), locs)
	}

	// Enums
	for idx, proto := range file.GetEnumType() {
		fillTreeWithEnum(tree, key, proto, fmt.Sprintf("5,%d", idx), locs)
	}

	// Services
	for idx, proto := range file.GetService() {
		fillTreeWithService(tree, key, proto, fmt.Sprintf("6,%d", idx), locs)
	}
}
Esempio n. 2
0
// LintProtoFile takes a file name, proto file description, and a file.
// It checks the file for errors and writes them to the output file
func LintProtoFile(
	protoFile *descriptor.FileDescriptorProto,
	outFile io.WriteCloser,
) (int, error) {
	var (
		errors      = protoBufErrors{}
		protoSource = protoFile.GetSourceCodeInfo()
	)

	for i, v := range protoFile.GetMessageType() {
		errors.lintProtoMessage(int32(i), pathMessage, []int32{}, v)
	}

	for i, v := range protoFile.GetEnumType() {
		errors.lintProtoEnumType(int32(i), pathEnumType, []int32{}, v)
	}

	for i, v := range protoFile.GetService() {
		errors.lintProtoService(int32(i), v)
	}
	for _, v := range errors {
		line, col := v.getSourceLineNumber(protoSource)
		fmt.Fprintf(
			outFile,
			"%s:%d:%d: '%s' - %s\n",
			*protoFile.Name,
			line,
			col,
			v.errorString,
			linterErrors[v.errorCode],
		)
	}

	return len(errors), nil

}