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
- Variables
- func CSharpNamespace(packageName string) string
- func GoPackage(packageName string) string
- func GoPackageV2(packageName string) string
- func Intersection(one []string, two []string) []string
- func IsCamelCase(s string) bool
- func IsCapitalized(s string) bool
- func IsLowerSnakeCase(s string) bool
- func IsLowercase(s string) bool
- func IsUpperSnakeCase(s string) bool
- func IsUppercase(s string) bool
- func JavaOuterClassname(filename string) string
- func JavaPackage(packageName string) string
- func JavaPackagePrefixOverride(packageName string, prefixOverride string) string
- func MajorBetaVersion(packageName string) (uint64, uint64, bool)
- func MapToSortedSlice(m map[string]struct{}) []string
- func OBJCClassPrefix(packageName string) string
- func PHPNamespace(packageName string) string
- func SortFailures(failures []*Failure)
- func SortUniq(s []string) []string
- func SortUniqModify(s []string, modifier func(string) string) []string
- func SplitCamelCaseWord(s string) []string
- func SplitSnakeCaseWord(s string) []string
- func ToLowerSnakeCase(s string) string
- func ToUpperCamelCase(s string) string
- func ToUpperSnakeCase(s string) string
- type Failure
- type FailureField
- type FailureWriter
- type Printer
- type Transformer
- type TransformerOption
Constants ¶
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 ¶
var ( // DefaultFailureFields are the default FailureFields. DefaultFailureFields = []FailureField{ FailureFieldFilename, FailureFieldLine, FailureFieldColumn, FailureFieldMessage, } )
Functions ¶
func CSharpNamespace ¶
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 ¶
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 ¶
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 ¶
Intersection return the intersection between one and two, sorted and dropping empty strings.
func IsCamelCase ¶
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 ¶
IsCapitalized returns true if is not empty and the first letter is an uppercase character.
func IsLowerSnakeCase ¶
IsLowerSnakeCase returns true if s only contains lowercase letters, digits, and/or underscores. s MUST NOT begin or end with an underscore.
func IsLowercase ¶
IsLowercase returns true if s is not empty and is all lowercase.
func IsUpperSnakeCase ¶
IsUpperSnakeCase returns true if s only contains uppercase letters, digits, and/or underscores. s MUST NOT begin or end with an underscore.
func IsUppercase ¶
IsUppercase returns true if s is not empty and is all uppercase.
func JavaOuterClassname ¶
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 ¶
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 ¶
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 ¶
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 ¶
MapToSortedSlice returns the sorted keys of m.
func OBJCClassPrefix ¶
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 ¶
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 SortUniqModify ¶
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 ¶
SplitCamelCaseWord splits a CamelCase word into its parts.
If s is empty, returns nil. If s is not CamelCase, returns nil.
func SplitSnakeCaseWord ¶
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 ¶
ToLowerSnakeCase converts s to lower_snake_case.
func ToUpperCamelCase ¶
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 ¶
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.
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.
type FailureWriter ¶
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.
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().