Skip to content

Commit

Permalink
WIP - Python spike content
Browse files Browse the repository at this point in the history
  • Loading branch information
pgmitche committed Sep 1, 2023
1 parent 25c0c16 commit 5515d7a
Show file tree
Hide file tree
Showing 5 changed files with 876 additions and 557 deletions.
40 changes: 40 additions & 0 deletions private/bufpkg/bufplugin/bufplugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func PluginRegistryToProtoRegistryConfig(pluginRegistry *bufpluginconfig.Registr
} else if pluginRegistry.Swift != nil {
swiftConfig := SwiftRegistryConfigToProtoSwiftConfig(pluginRegistry.Swift)
registryConfig.RegistryConfig = &registryv1alpha1.RegistryConfig_SwiftConfig{SwiftConfig: swiftConfig}
} else if pluginRegistry.Python != nil {
pythonConfig := PythonRegistryConfigToProtoPythonConfig(pluginRegistry.Python)
registryConfig.RegistryConfig = &registryv1alpha1.RegistryConfig_PythonConfig{PythonConfig: pythonConfig}
}
return registryConfig, nil
}
Expand Down Expand Up @@ -250,10 +253,47 @@ func ProtoRegistryConfigToPluginRegistry(config *registryv1alpha1.RegistryConfig
return nil, err
}
registryConfig.Swift = swiftConfig
} else if protoPythonConfig := config.GetPythonConfig(); protoPythonConfig != nil {
pythonConfig, err := ProtoPythonConfigToPythonRegistryConfig(protoPythonConfig)
if err != nil {
return nil, err
}
registryConfig.Python = pythonConfig
}
return registryConfig, nil
}

func ProtoPythonConfigToPythonRegistryConfig(protoPythonConfig *registryv1alpha1.PythonConfig) (*bufpluginconfig.PythonRegistryConfig, error) {
pythonConfig := &bufpluginconfig.PythonRegistryConfig{}
runtimeLibs := protoPythonConfig.GetRuntimeLibraries()
if runtimeLibs != nil {
pythonConfig.Dependencies = make([]bufpluginconfig.PythonRegistryDependencyConfig, 0, len(runtimeLibs))
for _, runtimeLib := range runtimeLibs {
dependencyConfig := bufpluginconfig.PythonRegistryDependencyConfig{
Package: runtimeLib.GetPackage(),
Version: runtimeLib.GetVersion(),
}
pythonConfig.Dependencies = append(pythonConfig.Dependencies, dependencyConfig)
}
}
return pythonConfig, nil
}

func PythonRegistryConfigToProtoPythonConfig(pythonConfig *bufpluginconfig.PythonRegistryConfig) *registryv1alpha1.PythonConfig {
protoPythonConfig := &registryv1alpha1.PythonConfig{}
if pythonConfig.Dependencies != nil {
protoPythonConfig.RuntimeLibraries = make([]*registryv1alpha1.PythonConfig_RuntimeLibrary, 0, len(pythonConfig.Dependencies))
for _, dependency := range pythonConfig.Dependencies {
depConfig := &registryv1alpha1.PythonConfig_RuntimeLibrary{
Package: dependency.Package,
Version: dependency.Version,
}
protoPythonConfig.RuntimeLibraries = append(protoPythonConfig.RuntimeLibraries, depConfig)
}
}
return protoPythonConfig
}

func ProtoSwiftConfigToSwiftRegistryConfig(protoSwiftConfig *registryv1alpha1.SwiftConfig) (*bufpluginconfig.SwiftRegistryConfig, error) {
swiftConfig := &bufpluginconfig.SwiftRegistryConfig{}
runtimeLibs := protoSwiftConfig.GetRuntimeLibraries()
Expand Down
48 changes: 39 additions & 9 deletions private/bufpkg/bufplugin/bufpluginconfig/bufpluginconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,11 @@ type Config struct {
//
// Only one field will be set.
type RegistryConfig struct {
Go *GoRegistryConfig
NPM *NPMRegistryConfig
Maven *MavenRegistryConfig
Swift *SwiftRegistryConfig
Go *GoRegistryConfig
NPM *NPMRegistryConfig
Maven *MavenRegistryConfig
Swift *SwiftRegistryConfig
Python *PythonRegistryConfig
// Options is the set of options passed into the plugin for the
// remote registry.
//
Expand Down Expand Up @@ -227,6 +228,20 @@ type SwiftRegistryDependencyPlatformConfig struct {
WatchOS string
}

// PythonRegistryConfig is the registry configuration for a Python plugin.
type PythonRegistryConfig struct {
// Dependencies are dependencies for the remote package.
Dependencies []PythonRegistryDependencyConfig
}

// PythonRegistryDependencyConfig is the python registry dependency configuration.
type PythonRegistryDependencyConfig struct {
// Package is the name of the Swift package.
Package string
// Version is the version of the Swift package.
Version string
}

// ConfigOption is an optional option used when loading a Config.
type ConfigOption func(*configOptions)

Expand Down Expand Up @@ -351,11 +366,12 @@ type ExternalDependency struct {
// ExternalRegistryConfig is the external configuration for the registry
// of a plugin.
type ExternalRegistryConfig struct {
Go *ExternalGoRegistryConfig `json:"go,omitempty" yaml:"go,omitempty"`
NPM *ExternalNPMRegistryConfig `json:"npm,omitempty" yaml:"npm,omitempty"`
Maven *ExternalMavenRegistryConfig `json:"maven,omitempty" yaml:"maven,omitempty"`
Swift *ExternalSwiftRegistryConfig `json:"swift,omitempty" yaml:"swift,omitempty"`
Opts []string `json:"opts,omitempty" yaml:"opts,omitempty"`
Go *ExternalGoRegistryConfig `json:"go,omitempty" yaml:"go,omitempty"`
NPM *ExternalNPMRegistryConfig `json:"npm,omitempty" yaml:"npm,omitempty"`
Maven *ExternalMavenRegistryConfig `json:"maven,omitempty" yaml:"maven,omitempty"`
Swift *ExternalSwiftRegistryConfig `json:"swift,omitempty" yaml:"swift,omitempty"`
Python *ExternalPythonRegistryConfig `json:"python,omitempty" yaml:"python,omitempty"`
Opts []string `json:"opts,omitempty" yaml:"opts,omitempty"`
}

// ExternalGoRegistryConfig is the external registry configuration for a Go plugin.
Expand Down Expand Up @@ -463,6 +479,20 @@ type ExternalSwiftRegistryDependencyPlatformConfig struct {
WatchOS string `json:"watchos,omitempty" yaml:"watchos,omitempty"`
}

type ExternalPythonRegistryConfig struct {
// Deps are dependencies for the remote package.
Deps []ExternalPythonRegistryDependencyConfig `json:"deps,omitempty" yaml:"deps,omitempty"`
}

type ExternalPythonRegistryDependencyConfig struct {
// Source is the URL of the Python package.
Source string `json:"source,omitempty" yaml:"source,omitempty"`
// Package is the name of the Swift package.
Package string `json:"package,omitempty" yaml:"package,omitempty"`
// Version is the version of the Swift package.
Version string `json:"version,omitempty" yaml:"version,omitempty"`
}

type externalConfigVersion struct {
Version string `json:"version,omitempty" yaml:"version,omitempty"`
}
Expand Down
55 changes: 51 additions & 4 deletions private/bufpkg/bufplugin/bufpluginconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,11 @@ func newConfig(externalConfig ExternalConfig, options []ConfigOption) (*Config,

func newRegistryConfig(externalRegistryConfig ExternalRegistryConfig) (*RegistryConfig, error) {
var (
isGoEmpty = externalRegistryConfig.Go == nil
isNPMEmpty = externalRegistryConfig.NPM == nil
isMavenEmpty = externalRegistryConfig.Maven == nil
isSwiftEmpty = externalRegistryConfig.Swift == nil
isGoEmpty = externalRegistryConfig.Go == nil
isNPMEmpty = externalRegistryConfig.NPM == nil
isMavenEmpty = externalRegistryConfig.Maven == nil
isSwiftEmpty = externalRegistryConfig.Swift == nil
isPythonEmpty = externalRegistryConfig.Python == nil
)
var registryCount int
for _, isEmpty := range []bool{
Expand Down Expand Up @@ -149,6 +150,15 @@ func newRegistryConfig(externalRegistryConfig ExternalRegistryConfig) (*Registry
Swift: swiftRegistryConfig,
Options: options,
}, nil
case !isPythonEmpty:
pythonRegistryConfig, err := newPythonRegistryConfig(externalRegistryConfig.Python)
if err != nil {
return nil, err
}
return &RegistryConfig{
Python: pythonRegistryConfig,
Options: options,
}, nil
default:
return nil, errors.New("unknown registry configuration")
}
Expand Down Expand Up @@ -322,6 +332,43 @@ func swiftExternalDependencyToDependencyConfig(externalDep ExternalSwiftRegistry
}, nil
}

func newPythonRegistryConfig(externalPythonRegistryConfig *ExternalPythonRegistryConfig) (*PythonRegistryConfig, error) {
if externalPythonRegistryConfig == nil {
return nil, nil
}
var dependencies []PythonRegistryDependencyConfig
for _, externalDependency := range externalPythonRegistryConfig.Deps {
dependency, err := pythonExternalDependencyToDependencyConfig(externalDependency)
if err != nil {
return nil, err
}
dependencies = append(dependencies, dependency)
}
return &PythonRegistryConfig{
Dependencies: dependencies,
}, nil
}

func pythonExternalDependencyToDependencyConfig(externalDep ExternalPythonRegistryDependencyConfig) (PythonRegistryDependencyConfig, error) {
if externalDep.Source == "" {
return PythonRegistryDependencyConfig{}, errors.New("python runtime dependency requires a non-empty source")
}
if externalDep.Package == "" {
return PythonRegistryDependencyConfig{}, errors.New("python runtime dependency requires a non-empty package name")
}
if externalDep.Version == "" {
return PythonRegistryDependencyConfig{}, errors.New("python runtime dependency requires a non-empty version name")
}
// Swift SemVers are typically not prefixed with a "v". The Golang semver library requires a "v" prefix.
if !semver.IsValid(fmt.Sprintf("v%s", externalDep.Version)) {
return PythonRegistryDependencyConfig{}, fmt.Errorf("python runtime dependency %s:%s does not have a valid semantic version", externalDep.Package, externalDep.Version)
}
return PythonRegistryDependencyConfig{
Package: externalDep.Package,
Version: externalDep.Version,
}, nil
}

func pluginIdentityForStringWithOverrideRemote(identityStr string, overrideRemote string) (bufpluginref.PluginIdentity, error) {
identity, err := bufpluginref.PluginIdentityForString(identityStr)
if err != nil {
Expand Down

0 comments on commit 5515d7a

Please sign in to comment.