protoparse

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 16, 2024 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Overview

Package protoparse provides function to parse proto source files and identify the go_api_flag file and message options specified in the files.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIInfo

type APIInfo struct {
	TextRange         TextRange
	HasLeadingComment bool
	// contains filtered or unexported fields
}

APIInfo contains information about an explicit API flag definition.

type FileOpt

type FileOpt struct {
	// File name containing relative path
	File string
	// Proto package name.
	Package string
	// Go API level. This can be an implicit value via default.
	GoAPI gofeaturespb.GoFeatures_APILevel
	// Whether go_api_flag option is explicitly set in proto file or not.
	IsExplicit bool
	// APIInfo is nil if IsExplicit is false.
	APIInfo *APIInfo
	// Options of messages defined at the file level. Nested messages are stored
	// as their children.
	MessageOpts []*MessageOpt
	// SourceCodeInfo is set if parsed results includes it.
	SourceCodeInfo *descpb.SourceCodeInfo
	// Proto syntax: "proto2", "proto3", "editions", or "editions_go_api_flag".
	// The latter is set for editions protos that use the old go_api_flag
	// explicitly.
	Syntax string
}

FileOpt contains the Go API level info for a file along with other proto info.

type MessageOpt

type MessageOpt struct {
	// Proto message name. Includes parent name if nested, e.g. A.B for message
	// B that is defined in body of A.
	Message string
	// Go API level. This can be an implicit value via file option or in case of
	// editions features via the parent message.
	GoAPI gofeaturespb.GoFeatures_APILevel
	// Whether go_api_flag option is explicitly set in proto message or not.
	IsExplicit bool
	// APIInfo is nil if IsExplicit is false.
	APIInfo *APIInfo
	// FileDescriptorProto.source_code_info.location.path of this message:
	// https://github.com/protocolbuffers/protobuf/blob/v29.1/src/google/protobuf/descriptor.proto#L1202
	// Example: The 1st nested message of the 6th message in the file is in path
	// [4, 5, 3, 0]; 4 is the field number of FileDescriptorProto.message_type, 5
	// is the index for the 6th message, 3 is DescriptorProto.nested_type, 0 is
	// the index for the first nested message.
	LocPath []int32
	// Options of the parent message. If this is e.g. the message B which is
	// defined in the body of message A, then A is the parent. Parent is nil for
	// messages defined at the file level, i.e. non-nested messages.
	Parent *MessageOpt
	// Options of the child messages. If this is e.g. message A and messages
	// B and C are defined in the body of message A, then B and C are the
	// children.
	Children []*MessageOpt
}

MessageOpt contains the Go API level info for a message.

type Parser

type Parser struct {
	// contains filtered or unexported fields
}

Parser parses proto source files for go_api_flag values.

func NewParser

func NewParser() *Parser

NewParser constructs a Parser with default file accessor.

func NewParserWithAccessor

func NewParserWithAccessor(acc protoparse.FileAccessor) *Parser

NewParserWithAccessor constructs a Parser with a custom file accessor.

func (*Parser) ParseFile

func (p *Parser) ParseFile(name string, skipMessages bool) (*FileOpt, error)

ParseFile reads the given proto source file name and determines the API level. If skipMessages is set to true, return value will have nil MessageOpts field.

type TextRange

type TextRange struct {
	BeginLine int
	BeginCol  int
	EndLine   int
	EndCol    int
}

TextRange describes a location in a proto file. Please note that the column indices are code-point indices, not byte indices.

func SpanToTextRange

func SpanToTextRange(span []int32) TextRange

SpanToTextRange converts a proto2.SourceCodeInfo.Location.span to a TextRange.

func (TextRange) ToByteRange

func (tr TextRange) ToByteRange(content []byte) (beginByte, endByte int, err error)

ToByteRange converts line and column information to a byte range.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL