bufgen

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2022 License: Apache-2.0 Imports: 31 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
}

Config is a configuration.

func ReadConfig

func ReadConfig(
	ctx context.Context,
	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 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"`
}

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 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"`
	OptimizeFor         string                            `json:"optimize_for,omitempty" yaml:"optimize_for,omitempty"`
	GoPackagePrefix     ExternalGoPackagePrefixConfigV1   `json:"go_package_prefix,omitempty" yaml:"go_package_prefix,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 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 {
	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     string      `json:"path,omitempty" yaml:"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 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,
	registryProvider registryv1alpha1apiclient.Provider,
) 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
	JavaPackagePrefix     *JavaPackagePrefixConfig
	OptimizeFor           *descriptorpb.FileOptions_OptimizeMode
	GoPackagePrefixConfig *GoPackagePrefixConfig
	Override              map[string]map[string]string
}

ManagedConfig is the managed mode configuration.

type PluginConfig

type PluginConfig struct {
	// One of Name and Remote is required
	Name   string
	Remote string
	// Required
	Out string
	// Optional
	Opt string
	// Optional, exclusive with Remote
	Path string
	// Required
	Strategy Strategy
}

PluginConfig is a plugin configuration.

func (*PluginConfig) PluginName added in v1.0.0

func (p *PluginConfig) PluginName() string

PluginName returns this PluginConfig's plugin name. Only one of 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 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.

Jump to

Keyboard shortcuts

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