bufgen

package
v1.30.1 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: Apache-2.0 Imports: 39 Imported by: 4

Documentation

Overview

Package bufgen does configuration-based generation.

It is used by the buf generate command.

Index

Constants

View Source
const (
	// ExternalConfigFilePath is the default external configuration file path.
	ExternalConfigFilePath = "buf.gen.yaml"
	// V1Version is the string used to identify the v1 version of the generate template.
	V1Version = "v1"
	// V1Beta1Version is the string used to identify the v1beta1 version of the generate template.
	V1Beta1Version = "v1beta1"
)

Variables

This section is empty.

Functions

func ConfigExists

func ConfigExists(ctx context.Context, readBucket storage.ReadBucket) (bool, error)

ConfigExists checks if a generation configuration file exists.

Types

type Config

type Config struct {
	// Required
	PluginConfigs []*PluginConfig
	// Optional
	ManagedConfig *ManagedConfig
	// Optional
	TypesConfig *TypesConfig
}

Config is a configuration.

func ReadConfig

func ReadConfig(
	ctx context.Context,
	logger *zap.Logger,
	provider Provider,
	readBucket storage.ReadBucket,
	options ...ReadConfigOption,
) (*Config, error)

ReadConfig reads the configuration from the OS or an override, if any.

Only use in CLI tools.

type CsharpNameSpaceConfig added in v1.10.0

type CsharpNameSpaceConfig struct {
	Except []bufmoduleref.ModuleIdentity
	// bufmoduleref.ModuleIdentity -> csharp_namespace prefix.
	Override map[bufmoduleref.ModuleIdentity]string
}

CsharpNameSpaceConfig is the csharp_namespace configuration.

type ExternalConfigV1

type ExternalConfigV1 struct {
	Version string                   `json:"version,omitempty" yaml:"version,omitempty"`
	Plugins []ExternalPluginConfigV1 `json:"plugins,omitempty" yaml:"plugins,omitempty"`
	Managed ExternalManagedConfigV1  `json:"managed,omitempty" yaml:"managed,omitempty"`
	Types   ExternalTypesConfigV1    `json:"types,omitempty" yaml:"types,omitempty"`
}

ExternalConfigV1 is an external configuration.

type ExternalConfigV1Beta1

type ExternalConfigV1Beta1 struct {
	Version string                        `json:"version,omitempty" yaml:"version,omitempty"`
	Managed bool                          `json:"managed,omitempty" yaml:"managed,omitempty"`
	Plugins []ExternalPluginConfigV1Beta1 `json:"plugins,omitempty" yaml:"plugins,omitempty"`
	Options ExternalOptionsConfigV1Beta1  `json:"options,omitempty" yaml:"options,omitempty"`
}

ExternalConfigV1Beta1 is an external configuration.

type ExternalConfigVersion

type ExternalConfigVersion struct {
	Version string `json:"version,omitempty" yaml:"version,omitempty"`
}

ExternalConfigVersion defines the subset of all config file versions that is used to determine the configuration version.

type ExternalCsharpNamespaceConfigV1 added in v1.10.0

type ExternalCsharpNamespaceConfigV1 struct {
	Except   []string          `json:"except,omitempty" yaml:"except,omitempty"`
	Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalCsharpNamespaceConfigV1 is the external csharp_namespace configuration.

func (ExternalCsharpNamespaceConfigV1) IsEmpty added in v1.10.0

IsEmpty returns true if the config is empty.

type ExternalGoPackagePrefixConfigV1

type ExternalGoPackagePrefixConfigV1 struct {
	Default  string            `json:"default,omitempty" yaml:"default,omitempty"`
	Except   []string          `json:"except,omitempty" yaml:"except,omitempty"`
	Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalGoPackagePrefixConfigV1 is the external go_package prefix configuration.

func (ExternalGoPackagePrefixConfigV1) IsEmpty

IsEmpty returns true if the config is empty.

type ExternalJavaPackagePrefixConfigV1 added in v1.0.0

type ExternalJavaPackagePrefixConfigV1 struct {
	Default  string            `json:"default,omitempty" yaml:"default,omitempty"`
	Except   []string          `json:"except,omitempty" yaml:"except,omitempty"`
	Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalJavaPackagePrefixConfigV1 is the external java_package prefix configuration.

func (ExternalJavaPackagePrefixConfigV1) IsEmpty added in v1.0.0

IsEmpty returns true if the config is empty.

func (*ExternalJavaPackagePrefixConfigV1) UnmarshalJSON added in v1.0.0

func (e *ExternalJavaPackagePrefixConfigV1) UnmarshalJSON(data []byte) error

UnmarshalJSON satisfies the json.Unmarshaler interface. This is done to maintain backward compatibility of accepting a plain string value for java_package_prefix.

func (*ExternalJavaPackagePrefixConfigV1) UnmarshalYAML added in v1.0.0

func (e *ExternalJavaPackagePrefixConfigV1) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML satisfies the yaml.Unmarshaler interface. This is done to maintain backward compatibility of accepting a plain string value for java_package_prefix.

type ExternalManagedConfigV1

type ExternalManagedConfigV1 struct {
	Enabled             bool                              `json:"enabled,omitempty" yaml:"enabled,omitempty"`
	CcEnableArenas      *bool                             `json:"cc_enable_arenas,omitempty" yaml:"cc_enable_arenas,omitempty"`
	JavaMultipleFiles   *bool                             `json:"java_multiple_files,omitempty" yaml:"java_multiple_files,omitempty"`
	JavaStringCheckUtf8 *bool                             `json:"java_string_check_utf8,omitempty" yaml:"java_string_check_utf8,omitempty"`
	JavaPackagePrefix   ExternalJavaPackagePrefixConfigV1 `json:"java_package_prefix,omitempty" yaml:"java_package_prefix,omitempty"`
	CsharpNamespace     ExternalCsharpNamespaceConfigV1   `json:"csharp_namespace,omitempty" yaml:"csharp_namespace,omitempty"`
	OptimizeFor         ExternalOptimizeForConfigV1       `json:"optimize_for,omitempty" yaml:"optimize_for,omitempty"`
	GoPackagePrefix     ExternalGoPackagePrefixConfigV1   `json:"go_package_prefix,omitempty" yaml:"go_package_prefix,omitempty"`
	ObjcClassPrefix     ExternalObjcClassPrefixConfigV1   `json:"objc_class_prefix,omitempty" yaml:"objc_class_prefix,omitempty"`
	RubyPackage         ExternalRubyPackageConfigV1       `json:"ruby_package,omitempty" yaml:"ruby_package,omitempty"`
	Override            map[string]map[string]string      `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalManagedConfigV1 is an external managed mode configuration.

Only use outside of this package for testing.

func (ExternalManagedConfigV1) IsEmpty

func (e ExternalManagedConfigV1) IsEmpty() bool

IsEmpty returns true if the config is empty, excluding the 'Enabled' setting.

type ExternalObjcClassPrefixConfigV1 added in v1.12.0

type ExternalObjcClassPrefixConfigV1 struct {
	Default  string            `json:"default,omitempty" yaml:"default,omitempty"`
	Except   []string          `json:"except,omitempty" yaml:"except,omitempty"`
	Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalObjcClassPrefixConfigV1 is the external objc_class_prefix configuration.

func (ExternalObjcClassPrefixConfigV1) IsEmpty added in v1.12.0

type ExternalOptimizeForConfigV1 added in v1.11.0

type ExternalOptimizeForConfigV1 struct {
	Default  string            `json:"default,omitempty" yaml:"default,omitempty"`
	Except   []string          `json:"except,omitempty" yaml:"except,omitempty"`
	Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalOptimizeForConfigV1 is the external optimize_for configuration.

func (ExternalOptimizeForConfigV1) IsEmpty added in v1.11.0

func (e ExternalOptimizeForConfigV1) IsEmpty() bool

IsEmpty returns true if the config is empty

func (*ExternalOptimizeForConfigV1) UnmarshalJSON added in v1.11.0

func (e *ExternalOptimizeForConfigV1) UnmarshalJSON(data []byte) error

UnmarshalJSON satisfies the json.Unmarshaler interface. This is done to maintain backward compatibility of accepting a plain string value for optimize_for.

func (*ExternalOptimizeForConfigV1) UnmarshalYAML added in v1.11.0

func (e *ExternalOptimizeForConfigV1) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML satisfies the yaml.Unmarshaler interface. This is done to maintain backward compatibility of accepting a plain string value for optimize_for.

type ExternalOptionsConfigV1Beta1

type ExternalOptionsConfigV1Beta1 struct {
	CcEnableArenas    *bool  `json:"cc_enable_arenas,omitempty" yaml:"cc_enable_arenas,omitempty"`
	JavaMultipleFiles *bool  `json:"java_multiple_files,omitempty" yaml:"java_multiple_files,omitempty"`
	OptimizeFor       string `json:"optimize_for,omitempty" yaml:"optimize_for,omitempty"`
}

ExternalOptionsConfigV1Beta1 is an external options configuration.

type ExternalPluginConfigV1

type ExternalPluginConfigV1 struct {
	Plugin     string      `json:"plugin,omitempty" yaml:"plugin,omitempty"`
	Revision   int         `json:"revision,omitempty" yaml:"revision,omitempty"`
	Name       string      `json:"name,omitempty" yaml:"name,omitempty"`
	Remote     string      `json:"remote,omitempty" yaml:"remote,omitempty"`
	Out        string      `json:"out,omitempty" yaml:"out,omitempty"`
	Opt        interface{} `json:"opt,omitempty" yaml:"opt,omitempty"`
	Path       interface{} `json:"path,omitempty" yaml:"path,omitempty"`
	ProtocPath string      `json:"protoc_path,omitempty" yaml:"protoc_path,omitempty"`
	Strategy   string      `json:"strategy,omitempty" yaml:"strategy,omitempty"`
}

ExternalPluginConfigV1 is an external plugin configuration.

type ExternalPluginConfigV1Beta1

type ExternalPluginConfigV1Beta1 struct {
	Name     string      `json:"name,omitempty" yaml:"name,omitempty"`
	Out      string      `json:"out,omitempty" yaml:"out,omitempty"`
	Opt      interface{} `json:"opt,omitempty" yaml:"opt,omitempty"`
	Path     string      `json:"path,omitempty" yaml:"path,omitempty"`
	Strategy string      `json:"strategy,omitempty" yaml:"strategy,omitempty"`
}

ExternalPluginConfigV1Beta1 is an external plugin configuration.

type ExternalRubyPackageConfigV1 added in v1.12.0

type ExternalRubyPackageConfigV1 struct {
	Except   []string          `json:"except,omitempty" yaml:"except,omitempty"`
	Override map[string]string `json:"override,omitempty" yaml:"override,omitempty"`
}

ExternalRubyPackageConfigV1 is the external ruby_package configuration

func (ExternalRubyPackageConfigV1) IsEmpty added in v1.12.0

func (e ExternalRubyPackageConfigV1) IsEmpty() bool

IsEmpty returns true is the config is empty

type ExternalTypesConfigV1 added in v1.10.0

type ExternalTypesConfigV1 struct {
	Include []string `json:"include,omitempty" yaml:"include"`
}

ExternalTypesConfigV1 is an external types configuration.

func (ExternalTypesConfigV1) IsEmpty added in v1.18.0

func (e ExternalTypesConfigV1) IsEmpty() bool

IsEmpty returns true if e is empty.

type GenerateOption

type GenerateOption func(*generateOptions)

GenerateOption is an option for Generate.

func GenerateWithBaseOutDirPath

func GenerateWithBaseOutDirPath(baseOutDirPath string) GenerateOption

GenerateWithBaseOutDirPath returns a new GenerateOption that uses the given base directory as the output directory.

The default is to use the current directory.

func GenerateWithIncludeImports

func GenerateWithIncludeImports() GenerateOption

GenerateWithIncludeImports says to also generate imports.

Note that this does NOT result in the Well-Known Types being generated, use GenerateWithIncludeWellKnownTypes to include the Well-Known Types.

func GenerateWithIncludeWellKnownTypes added in v1.0.0

func GenerateWithIncludeWellKnownTypes() GenerateOption

GenerateWithIncludeWellKnownTypes says to also generate well known types.

This option has no effect if GenerateWithIncludeImports is not set.

type Generator

type Generator interface {
	// Generate calls the generation logic.
	//
	// The config is assumed to be valid. If created by ReadConfig, it will
	// always be valid.
	Generate(
		ctx context.Context,
		container app.EnvStdioContainer,
		config *Config,
		image bufimage.Image,
		options ...GenerateOption,
	) error
}

Generator generates Protobuf stubs based on configurations.

func NewGenerator

func NewGenerator(
	logger *zap.Logger,
	storageosProvider storageos.Provider,
	runner command.Runner,
	clientConfig *connectclient.Config,
) Generator

NewGenerator returns a new Generator.

type GoPackagePrefixConfig

type GoPackagePrefixConfig struct {
	Default string
	Except  []bufmoduleref.ModuleIdentity
	// bufmoduleref.ModuleIdentity -> go_package prefix.
	Override map[bufmoduleref.ModuleIdentity]string
}

GoPackagePrefixConfig is the go_package prefix configuration.

type JavaPackagePrefixConfig added in v1.0.0

type JavaPackagePrefixConfig struct {
	Default string
	Except  []bufmoduleref.ModuleIdentity
	// bufmoduleref.ModuleIdentity -> java_package prefix.
	Override map[bufmoduleref.ModuleIdentity]string
}

JavaPackagePrefixConfig is the java_package prefix configuration.

type ManagedConfig

type ManagedConfig struct {
	CcEnableArenas          *bool
	JavaMultipleFiles       *bool
	JavaStringCheckUtf8     *bool
	JavaPackagePrefixConfig *JavaPackagePrefixConfig
	CsharpNameSpaceConfig   *CsharpNameSpaceConfig
	OptimizeForConfig       *OptimizeForConfig
	GoPackagePrefixConfig   *GoPackagePrefixConfig
	ObjcClassPrefixConfig   *ObjcClassPrefixConfig
	RubyPackageConfig       *RubyPackageConfig
	Override                map[string]map[string]string
}

ManagedConfig is the managed mode configuration.

type ObjcClassPrefixConfig added in v1.12.0

type ObjcClassPrefixConfig struct {
	Default string
	Except  []bufmoduleref.ModuleIdentity
	// bufmoduleref.ModuleIdentity -> objc_class_prefix.
	Override map[bufmoduleref.ModuleIdentity]string
}

ObjcClassPrefixConfig is the objc_class_prefix configuration.

type OptimizeForConfig added in v1.11.0

type OptimizeForConfig struct {
	Default descriptorpb.FileOptions_OptimizeMode
	Except  []bufmoduleref.ModuleIdentity
	// bufmoduleref.ModuleIdentity -> optimize_for.
	Override map[bufmoduleref.ModuleIdentity]descriptorpb.FileOptions_OptimizeMode
}

type PluginConfig

type PluginConfig struct {
	// One of Plugin, Name or Remote is required
	Plugin string
	Name   string
	Remote string
	// Optional, used with Plugin to pin a specific revision
	Revision int
	// Required
	Out string
	// Optional
	Opt string
	// Optional, exclusive with Remote
	Path []string
	// Required
	Strategy Strategy
	// Optional
	ProtocPath string
}

PluginConfig is a plugin configuration.

func (*PluginConfig) GetRemoteHostname added in v1.11.0

func (p *PluginConfig) GetRemoteHostname() string

GetRemoteHostname returns the hostname of the remote plugin.

func (*PluginConfig) IsRemote added in v1.8.0

func (p *PluginConfig) IsRemote() bool

IsRemote returns true if the PluginConfig uses a remotely executed plugin.

func (*PluginConfig) PluginName added in v1.0.0

func (p *PluginConfig) PluginName() string

PluginName returns this PluginConfig's plugin name. Only one of Plugin, Name or Remote will be set.

type Provider

type Provider interface {
	// GetConfig gets the Config for the YAML data at ExternalConfigFilePath.
	//
	// If the data is of length 0, returns the default config.
	GetConfig(ctx context.Context, readBucket storage.ReadBucket) (*Config, error)
}

Provider is a provider.

func NewProvider

func NewProvider(logger *zap.Logger) Provider

NewProvider returns a new Provider.

type ReadConfigOption

type ReadConfigOption func(*readConfigOptions)

ReadConfigOption is an option for ReadConfig.

func ReadConfigWithOverride

func ReadConfigWithOverride(override string) ReadConfigOption

ReadConfigWithOverride sets the override.

If override is set, this will first check if the override ends in .json or .yaml, if so, this reads the file at this path and uses it. Otherwise, this assumes this is configuration data in either JSON or YAML format, and unmarshals it.

If no override is set, this reads ExternalConfigFilePath in the bucket.

type RubyPackageConfig added in v1.12.0

type RubyPackageConfig struct {
	Except []bufmoduleref.ModuleIdentity
	// bufmoduleref.ModuleIdentity -> ruby_package.
	Override map[bufmoduleref.ModuleIdentity]string
}

RubyPackgeConfig is the ruby_package configuration.

type Strategy

type Strategy int

Strategy is a generation stategy.

const (
	// StrategyDirectory is the strategy that says to generate per directory.
	//
	// This is the default value.
	StrategyDirectory Strategy = 1
	// StrategyAll is the strategy that says to generate with all files at once.
	StrategyAll Strategy = 2
)

func ParseStrategy

func ParseStrategy(s string) (Strategy, error)

ParseStrategy parses the Strategy.

If the empty string is provided, this is interpreted as StrategyDirectory.

func (Strategy) String

func (s Strategy) String() string

String implements fmt.Stringer.

type TypesConfig added in v1.10.0

type TypesConfig struct {
	Include []string
}

TypesConfig is a types configuration

Jump to

Keyboard shortcuts

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