pulumi/pkg/codegen/go/importer.go

147 lines
6.4 KiB
Go
Raw Permalink Normal View History

// Copyright 2016-2021, Pulumi Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gen
import (
"encoding/json"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
)
// GoPackageInfo holds information required to generate the Go SDK from a schema.
type GoPackageInfo struct {
// Base path for package imports
//
// github.com/pulumi/pulumi-kubernetes/sdk/go/kubernetes
ImportBasePath string `json:"importBasePath,omitempty"`
2022-10-10 23:01:53 +00:00
// Module path for go.mod
//
// go get github.com/pulumi/pulumi-aws-native/sdk/go/aws@v0.16.0
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ module path
// ~~~~~~ package path - can be any number of path parts
// ~~~~~~~ version
ModulePath string `json:"modulePath,omitempty"`
// Explicit package name, which may be different to the import path.
RootPackageName string `json:"rootPackageName,omitempty"`
// Map from module -> package name
//
// { "flowcontrol.apiserver.k8s.io/v1alpha1": "flowcontrol/v1alpha1" }
//
ModuleToPackage map[string]string `json:"moduleToPackage,omitempty"`
// Map from package name -> package alias
//
// { "github.com/pulumi/pulumi-kubernetes/sdk/go/kubernetes/flowcontrol/v1alpha1": "flowcontrolv1alpha1" }
//
PackageImportAliases map[string]string `json:"packageImportAliases,omitempty"`
[go/program-gen] Implement importPathPattern in Go language options to override emitted paths in generated Go programs (#16267) # Description Partially addressing https://github.com/pulumi/pulumi-azure-native/issues/3308 Implementing a new go language option called `importPathPattern` which can be used to override how the base import path and modules are concatenated to create imports in generated Go programs. By convention this used to be `{baseImportPath}/{module}` which has worked for all of our providers. However, azure-native v2 has introduced a new import scheme where the convention above causes incorrect import paths to be generated. This is where `importPathPattern` comes into play and allows for a different convention. In the case the of azure-native v2, the pattern _must_ be `github.com/pulumi/pulumi-azure-native-sdk/{module}/v2`. This PR implements `importPathPattern` and tests it using a squashed down azure-native v2 schema containing only contents from the `eventgrid` module. This schema sets the option like this: ```json "importPathPattern": "github.com/pulumi/pulumi-azure-native-sdk/{module}/v2" ``` This schema also modifies `packageImportAliases` from the current azure-native v2 schema to exclude /v2 before the module path (see the file below). This change is needed in the actual azure-native v2 provider cc @danielrbradley and it is the second part of fully fixing https://github.com/pulumi/pulumi-azure-native/issues/3308 > We cannot just use `{baseImportPath}/{module}` because the base import path for azure-native v2 has a suffix v2 (it's required) Also implemented a small feature in ProgramTest to allow overriding the used plugin host for the specific program test, this is because I wanted to test the program against the simplified azure-native v2 schema without changing how other test load the previous azure-native v1.x schemas (I tested that without this, binding programs fails) ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [x] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [ ] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2024-05-30 19:51:12 +00:00
// Defines the pattern for how import paths should be constructed from the base import path and used module
// By default, the pattern is "{baseImportPath}/{module}" but can be overridden to support other patterns.
// for example you can use "{baseImportPath}/{module}/v2" which will replace {module} with the module name
// and {baseImportPath} with the base import path to compute the full path.
ImportPathPattern string `json:"importPathPattern,omitempty"`
// Generate container types (arrays, maps, pointer output types etc.) for each resource.
// These are typically used to support external references.
GenerateResourceContainerTypes bool `json:"generateResourceContainerTypes,omitempty"`
// The version of the Pulumi SDK used with this provider, e.g. 3.
// Used to generate doc links for pulumi builtin types. If omitted, the latest SDK version is used.
PulumiSDKVersion int `json:"pulumiSDKVersion,omitempty"`
// Feature flag to disable generating `$fnOutput` invoke
// function versions to save space.
DisableFunctionOutputVersions bool `json:"disableFunctionOutputVersions,omitempty"`
// Determines whether to make single-return-value methods return an output struct or the value.
LiftSingleValueMethodReturns bool `json:"liftSingleValueMethodReturns,omitempty"`
// Feature flag to disable generating input type registration. This is a
// space saving measure.
DisableInputTypeRegistrations bool `json:"disableInputTypeRegistrations,omitempty"`
[codegen/go] Call site defaults for Pulumi Object types (#8411) * Add test case * Fix tests * Add test dependencies correctly * Feed through error handling * Include test output * Get types to line up * Add remaining test files * Update changelog * Correctly find type paths * Handle transitive objects * Handle required fields * Add feature flag for go * Add required+default test case * Don't `<any>` cast known types. * Add more flags. I realize this should really wait for PR#8400 to merge. * Add plain object to env-helper test This test fails right now. My next problem is fixing it. * Handle plain types * Handle function inputs * Fix the indentation * Handle output types correctly * Remove unnecessary `!` * Add test case * Fix tests * Add test dependencies correctly * Feed through error handling * Include test output * Get types to line up * Add remaining test files * Update changelog * Correctly find type paths * Handle transitive objects * Handle required fields * Add required+default test case * Don't `<any>` cast known types. * Add plain object to env-helper test This test fails right now. My next problem is fixing it. * Handle plain types * Handle function inputs * Fix the indentation * Handle output types correctly * Remove unnecessary `!` * Start on `genPlainObjectDefaultFunc` * Add missing change to fix test * Run tests with merge * Refactor out assign * Merge in next _index.md diff * Change method name to `Defaults` * Handle enums correctly * Another attempt at _index.md * Make module generation deterministic * Add checks for old values * Insert defaults in resources * Fix docs generation Credit to @praneetloke * Progress on adding defaults to Resource arguments * Handle resource argument defaults * Don't create defaults if disableObjectDefaults * Rename test folder * Add test for disable flag * Fix disable test * Update docs * Abstract out nil comparisons * Use reflection to test for empty values * Simplify Ptr and pulumi.Any type handling * Remove unused function * Apply defaults to functions * Update new test with master codegen * Tests + nil check
2021-11-23 23:10:15 +00:00
// When set, the code generator will use this name for the generated internal module
// instead of "internal" so that functionality within the module can be used by end users.
InternalModuleName string `json:"internalModuleName,omitempty"`
[codegen/go] Call site defaults for Pulumi Object types (#8411) * Add test case * Fix tests * Add test dependencies correctly * Feed through error handling * Include test output * Get types to line up * Add remaining test files * Update changelog * Correctly find type paths * Handle transitive objects * Handle required fields * Add feature flag for go * Add required+default test case * Don't `<any>` cast known types. * Add more flags. I realize this should really wait for PR#8400 to merge. * Add plain object to env-helper test This test fails right now. My next problem is fixing it. * Handle plain types * Handle function inputs * Fix the indentation * Handle output types correctly * Remove unnecessary `!` * Add test case * Fix tests * Add test dependencies correctly * Feed through error handling * Include test output * Get types to line up * Add remaining test files * Update changelog * Correctly find type paths * Handle transitive objects * Handle required fields * Add required+default test case * Don't `<any>` cast known types. * Add plain object to env-helper test This test fails right now. My next problem is fixing it. * Handle plain types * Handle function inputs * Fix the indentation * Handle output types correctly * Remove unnecessary `!` * Start on `genPlainObjectDefaultFunc` * Add missing change to fix test * Run tests with merge * Refactor out assign * Merge in next _index.md diff * Change method name to `Defaults` * Handle enums correctly * Another attempt at _index.md * Make module generation deterministic * Add checks for old values * Insert defaults in resources * Fix docs generation Credit to @praneetloke * Progress on adding defaults to Resource arguments * Handle resource argument defaults * Don't create defaults if disableObjectDefaults * Rename test folder * Add test for disable flag * Fix disable test * Update docs * Abstract out nil comparisons * Use reflection to test for empty values * Simplify Ptr and pulumi.Any type handling * Remove unused function * Apply defaults to functions * Update new test with master codegen * Tests + nil check
2021-11-23 23:10:15 +00:00
// Feature flag to disable generating Pulumi object default functions. This is a
// space saving measure.
DisableObjectDefaults bool `json:"disableObjectDefaults,omitempty"`
// GenerateExtraInputTypes determines whether or not the code generator generates input (and output) types for
// all plain types, instead of for only types that are used as input/output types.
GenerateExtraInputTypes bool `json:"generateExtraInputTypes,omitempty"`
// omitExtraInputTypes determines whether the code generator generates input (and output) types
// for all plain types, instead of for only types that are used as input/output types.
OmitExtraInputTypes bool `json:"omitExtraInputTypes,omitempty"`
// Respect the Pkg.Version field for emitted code.
RespectSchemaVersion bool `json:"respectSchemaVersion,omitempty"`
// InternalDependencies are blank imports that are emitted in the SDK so that `go mod tidy` does not remove the
// associated module dependencies from the SDK's go.mod.
InternalDependencies []string `json:"internalDependencies,omitempty"`
[go/sdk-gen] Generating SDK with generics (#13828) # Description This PR implements generating the generic variant of a go SDK from Pulumi schemas. Currently the idea is to generate a directory `x` inside the root directory of the go SDK which will contain the same SDK except using generics and generating far less code than its current counter part. Also implements an enum option `$.language.go.generics` which can be set to the following: - `none` is the default which maintains the current behavior that generates legacy types without generics - `side-by-side` generates the generics sdk variant alongside the current sdk under directory `x` - `generics-only` generates only the new sdk with generics at the root of the package Still a bunch of things to do: - [x] Generating `InvokeResult]Output` type from `Output[InvokeResult]` and generating accessor methods for it - [x] Generating default values for types and using the `pulumix` subpackage to do so - [x] Generating generic SDK variants for all test schemas we have and making sure they compile (currently only testing `simple-resource-schema` as shown below) - [x] Account for plain inputs for components - [x] Combine pulumix.Join with pulumix.Apply to generate resource accessor methods - [x] Problem with `GPtrOutput[T]` and `ArrayOutput[T]` being unwrapped to `Output[*T]` and `Output[[]T]` - [x] Remove excess untyped container types from generated enums - [x] Fix default values for resource methods with lifted single return value - [x] Secret properties Currently the following test schemas have opted for `generics: "side-by-side"`: - [x] output-funcs - [x] simple-enum-schema - [x] secrets - [x] simple-plain-schema - [x] plain-and-default ## Checklist - [ ] I have run `make tidy` to update any new dependencies - [x] I have run `make lint` to verify my code passes the lint check - [ ] I have formatted my code using `gofumpt` <!--- Please provide details if the checkbox below is to be left unchecked. --> - [x] I have added tests that prove my fix is effective or that my feature works <!--- User-facing changes require a CHANGELOG entry. --> - [x] I have run `make changelog` and committed the `changelog/pending/<file>` documenting my change <!-- If the change(s) in this PR is a modification of an existing call to the Pulumi Cloud, then the service should honor older versions of the CLI where this change would not exist. You must then bump the API version in /pkg/backend/httpstate/client/api.go, as well as add it to the service. --> - [ ] Yes, there are changes in this PR that warrants bumping the Pulumi Cloud API version <!-- @Pulumi employees: If yes, you must submit corresponding changes in the service repo. -->
2023-09-19 10:28:50 +00:00
// Specifies how to handle generating a variant of the SDK that uses generics.
// Allowed values are the following:
// - "none" (default): do not generate a generics variant of the SDK
// - "side-by-side": generate a side-by-side generics variant of the SDK under the x subdirectory
// - "only-generics": generate a generics variant of the SDK only
Generics string `json:"generics,omitempty"`
}
// Importer implements schema.Language for Go.
var Importer schema.Language = importer(0)
type importer int
// ImportDefaultSpec decodes language-specific metadata associated with a DefaultValue.
func (importer) ImportDefaultSpec(def *schema.DefaultValue, raw json.RawMessage) (interface{}, error) {
return raw, nil
}
// ImportPropertySpec decodes language-specific metadata associated with a Property.
func (importer) ImportPropertySpec(property *schema.Property, raw json.RawMessage) (interface{}, error) {
return raw, nil
}
// ImportObjectTypeSpec decodes language-specific metadata associated with a ObjectType.
func (importer) ImportObjectTypeSpec(object *schema.ObjectType, raw json.RawMessage) (interface{}, error) {
return raw, nil
}
// ImportResourceSpec decodes language-specific metadata associated with a Resource.
func (importer) ImportResourceSpec(resource *schema.Resource, raw json.RawMessage) (interface{}, error) {
return raw, nil
}
// ImportFunctionSpec decodes language-specific metadata associated with a Function.
func (importer) ImportFunctionSpec(function *schema.Function, raw json.RawMessage) (interface{}, error) {
return raw, nil
}
// ImportPackageSpec decodes language-specific metadata associated with a Package.
func (importer) ImportPackageSpec(pkg *schema.Package, raw json.RawMessage) (interface{}, error) {
var info GoPackageInfo
if err := json.Unmarshal(raw, &info); err != nil {
return nil, err
}
return info, nil
}