proto

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2023 License: MIT, MIT Imports: 11 Imported by: 0

Documentation

Overview

Package protostrs contains common string manipulation functionality for Protobuf packages and files.

This is used in the format, lint, and create packages. The Java rules in this package roughly follow https://cloud.google.com/apis/design/file_structure.

Package strs contains common string manipulation functionality.

This functionality is not really centralized anywhere in Golang OSS world, and there are some specific requirements we have. This is used mostly in the lint package.

Index

Constants

View Source
const (
	// FixNone says to not do fixing.
	FixNone = 0
	// FixV1 says to do V1 fixing.
	FixV1 = 1
	// FixV2 says to do V2 fixing.
	FixV2 = 2
)

Variables

View Source
var (
	// DefaultFailureFields are the default FailureFields.
	DefaultFailureFields = []FailureField{
		FailureFieldFilename,
		FailureFieldLine,
		FailureFieldColumn,
		FailureFieldMessage,
	}
)

Functions

func CSharpNamespace

func CSharpNamespace(packageName string) string

CSharpNamespace returns the value for the file option "csharp_namespace" given a package name. It will capitalize each part of the package name taking special care for beta packages.

func GoPackage

func GoPackage(packageName string) string

GoPackage returns the value for the file option "go_package" given a package name. This will be equal to the last value of the package separated by "."s, followed by "pb". If packageName is empty, this will return an empty string.

func GoPackageV2

func GoPackageV2(packageName string) string

GoPackageV2 returns the value for the file option "go_package" given a package name. This will be equal to the last two values of the package separated by "."s if the package is a MajorBetaPackage, or GoPackage otherwise.

func Intersection

func Intersection(one []string, two []string) []string

Intersection return the intersection between one and two, sorted and dropping empty strings.

func IsCamelCase

func IsCamelCase(s string) bool

IsCamelCase returns false if s is empty or contains any character that is not between 'A' and 'Z', 'a' and 'z', '0' and '9', or in extraRunes. It does not care about lowercase or uppercase.

func IsCapitalized

func IsCapitalized(s string) bool

IsCapitalized returns true if is not empty and the first letter is an uppercase character.

func IsLowerSnakeCase

func IsLowerSnakeCase(s string) bool

IsLowerSnakeCase returns true if s only contains lowercase letters, digits, and/or underscores. s MUST NOT begin or end with an underscore.

func IsLowercase

func IsLowercase(s string) bool

IsLowercase returns true if s is not empty and is all lowercase.

func IsUpperSnakeCase

func IsUpperSnakeCase(s string) bool

IsUpperSnakeCase returns true if s only contains uppercase letters, digits, and/or underscores. s MUST NOT begin or end with an underscore.

func IsUppercase

func IsUppercase(s string) bool

IsUppercase returns true if s is not empty and is all uppercase.

func JavaOuterClassname

func JavaOuterClassname(filename string) string

JavaOuterClassname returns the value for the file option "java_outer_classname" given a file name. This will be equal to the basename of the file with it's extension stripped, UpperCamelCased, followed by "Proto". If filename is empty, this will return an empty string.

func JavaPackage

func JavaPackage(packageName string) string

JavaPackage returns the value for the file option "java_package" given a package name. This will be equal to "com." followed by the package. If packageName is empty, this will return an empty string.

func JavaPackagePrefixOverride

func JavaPackagePrefixOverride(packageName string, prefixOverride string) string

JavaPackagePrefixOverride returns the value for the file option "java_package" given a package name. This will be equal to "com." followed by the package. If prefixOverride is set, this will be equal to prefixOveride.package. If packageName is empty, this will return an empty string.

func MajorBetaVersion

func MajorBetaVersion(packageName string) (uint64, uint64, bool)

MajorBetaVersion extracts the major and beta version number from the package name, if present. A package must be of the form "foo.vMAJORVERSION" or "foo.vMAJORVERSIONbetaBETAVERSION" . Returns the major version, beta version and true if the package is of this form, 0 and false otherwise. If there is no beta version, 0 is returned. Valid versions are >=1.

func MapToSortedSlice

func MapToSortedSlice(m map[string]struct{}) []string

MapToSortedSlice returns the sorted keys of m.

func OBJCClassPrefix

func OBJCClassPrefix(packageName string) string

OBJCClassPrefix returns the value for the file option "objc_class_prefix" given a package name. It takes the first letter of each package part and capitalizes it, then concatenates these. If the length is 2, an "X" is added. If the length is 1, "XX" is added. If the length is 0, this returns empty. If the name is "GPB", this returns "GPX". The version part is dropped before all operations.

func PHPNamespace

func PHPNamespace(packageName string) string

PHPNamespace returns the value for the file option "php_namespace" given a package name. It will capitalize each part of the package name taking special care for beta packages.

func SortFailures

func SortFailures(failures []*Failure)

SortFailures sorts the Failures, by filename, line, column, id, message.

func SortUniq

func SortUniq(s []string) []string

SortUniq returns the unique sorted non-empty values of s.

func SortUniqModify

func SortUniqModify(s []string, modifier func(string) string) []string

SortUniqModify returns the unique sorted non-empty values of s. If modifier is not nil, modifier will be applied to each element in s.

func SplitCamelCaseWord

func SplitCamelCaseWord(s string) []string

SplitCamelCaseWord splits a CamelCase word into its parts.

If s is empty, returns nil. If s is not CamelCase, returns nil.

func SplitSnakeCaseWord

func SplitSnakeCaseWord(s string) []string

SplitSnakeCaseWord splits a snake_case word into its parts.

If s is empty, returns nil. If s is not snake_case, returns nil.

func ToLowerSnakeCase

func ToLowerSnakeCase(s string) string

ToLowerSnakeCase converts s to lower_snake_case.

func ToUpperCamelCase

func ToUpperCamelCase(s string) string

ToUpperCamelCase converts s to UpperCamelCase.

We use this for files, so any delimiter (_, -, or space) is used to denote word boundaries, but we trim spaces from the beginning and end of the string first.

If a letter is uppercase, it will stay uppercase regardless, this is for cases of abbreviations.

func ToUpperSnakeCase

func ToUpperSnakeCase(s string) string

ToUpperSnakeCase converts s to UPPER_SNAKE_CASE.

Types

type Failure

type Failure struct {
	Filename string `json:"filename,omitempty"`
	Line     int    `json:"line,omitempty"`
	Column   int    `json:"column,omitempty"`
	LintID   string `json:"lint_id,omitempty"`
	Message  string `json:"message,omitempty"`
}

Failure is a failure with a position in text.

func NewFailuref

func NewFailuref(position scanner.Position, lintID string, format string, args ...interface{}) *Failure

NewFailuref is a helper that returns a new Failure.

func (*Failure) Fprintln

func (f *Failure) Fprintln(writer FailureWriter, fields ...FailureField) error

Fprintln prints the Failure to the writer with the given ordered fields.

func (*Failure) String

func (f *Failure) String() string

String implements fmt.Stringer.

type FailureField

type FailureField int

FailureField references a field of a Failure.

const (
	// FailureFieldFilename references the Filename field of a Failure.
	FailureFieldFilename FailureField = iota
	// FailureFieldLine references the Line field of a Failure.
	FailureFieldLine
	// FailureFieldColumn references the Column field of a Failure.
	FailureFieldColumn
	// FailureFieldID references the ID field of a Failure.
	FailureFieldID
	// FailureFieldMessage references the Message field of a Failure.
	FailureFieldMessage
)

func ParseColonSeparatedFailureFields

func ParseColonSeparatedFailureFields(s string) ([]FailureField, error)

ParseColonSeparatedFailureFields parses FailureFields from the given string. FailureFields are expected to be colon-separated in the given string. Input is case-insensitive. If the string is empty, DefaultFailureFields will be returned.

func ParseFailureField

func ParseFailureField(s string) (FailureField, error)

ParseFailureField parses the FailureField from the given string.

Input is case-insensitive.

func (FailureField) String

func (f FailureField) String() string

String implements fmt.Stringer.

type FailureWriter

type FailureWriter interface {
	WriteRune(rune) (int, error)
	WriteString(string) (int, error)
}

FailureWriter is a writer that Failure.Println can accept.

Both bytes.Buffer and bufio.Writer implement this.

type Printer

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

Printer is a convenience struct that helps when printing files.

The concept was taken from the golang/protobuf plugin.

func NewPrinter

func NewPrinter(indent string) *Printer

NewPrinter returns a new Printer.

func (*Printer) Bytes

func (p *Printer) Bytes() []byte

Bytes returns the printed bytes.

func (*Printer) In

func (p *Printer) In()

In adds one indent.

func (*Printer) Out

func (p *Printer) Out()

Out deletes one indent.

func (*Printer) P

func (p *Printer) P(args ...interface{})

P prints the args concatenated on the same line after printing the current indent and then prints a newline.

func (*Printer) String

func (p *Printer) String() string

String returns the printed string.

type Transformer

type Transformer interface {
	// Transform transforms the data.
	//
	// Failures should never happen in the CLI tool as we run the files
	// through protoc first, but this is done because we want to verify
	// code correctness here and protect against the bad case.
	Transform(filename string, data []byte) ([]byte, []*Failure, error)
}

Transformer transforms an input file into an output file.

func NewTransformer

func NewTransformer(options ...TransformerOption) Transformer

NewTransformer returns a new Transformer.

type TransformerOption

type TransformerOption func(*transformer)

TransformerOption is an option for a new Transformer.

func TransformerWithFileHeader

func TransformerWithFileHeader(fileHeader string) TransformerOption

TransformerWithFileHeader returns a TransformerOption that will update the file header to match the given file header.

This is only valid if fix is set to a value other than FixNone.

func TransformerWithFix

func TransformerWithFix(fix int) TransformerOption

TransformerWithFix returns a TransformerOption that will update the file options to match the package per the guidelines of the style guide.

func TransformerWithJavaPackagePrefix

func TransformerWithJavaPackagePrefix(javaPackagePrefix string) TransformerOption

TransformerWithJavaPackagePrefix returns a TransformerOption that will override the Java package prefix.

This is only valid if fix is set to a value other than FixNone.

func TransformerWithLogger

func TransformerWithLogger(logger *zap.Logger) TransformerOption

TransformerWithLogger returns a TransformerOption that uses the given logger.

The default is to use zap.NewNop().

Jump to

Keyboard shortcuts

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