Skip to content

Commit

Permalink
Move main.TransformPackages to transformers.Resolver.TransformPackages
Browse files Browse the repository at this point in the history
- Bump Go version to 1.21
- Bump golangci-lint to v1.55.2
- Add tests for the Resolver.TransformPackages

Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Jan 25, 2024
1 parent bc7c179 commit 1493017
Show file tree
Hide file tree
Showing 9 changed files with 2,188 additions and 26 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ on:

env:
# Common versions
GO_VERSION: "1.20"
GOLANGCI_VERSION: "v1.53.3"
GO_VERSION: "1.21"
GOLANGCI_VERSION: "v1.55.2"
DOCKER_BUILDX_VERSION: "v0.8.2"

# Common users. We can't run a step 'if secrets.AWS_USR != ""' but we can run
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ PROJECT_REPO := github.com/crossplane/$(PROJECT_NAME)

# GOLANGCILINT_VERSION is inherited from build submodule by default.
# Uncomment below if you need to override the version.
# GOLANGCILINT_VERSION ?= 1.54.0
GO_REQUIRED_VERSION ?= 1.20
GOLANGCILINT_VERSION ?= 1.55.2
GO_REQUIRED_VERSION ?= 1.21

PLATFORMS ?= linux_amd64 linux_arm64
# -include will silently skip missing files, which allows us
Expand Down
11 changes: 9 additions & 2 deletions cmd/resolver/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0

Expand All @@ -8,7 +8,12 @@ import (
"os"
"path/filepath"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/spf13/afero"
"gopkg.in/alecthomas/kingpin.v2"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

"github.com/crossplane/upjet/pkg/transformers"
)

func main() {
Expand All @@ -20,5 +25,7 @@ func main() {
ignorePackageLoadErrors = app.Flag("ignoreLoadErrors", "Ignore errors encountered while loading the packages.").Short('s').Bool()
)
kingpin.MustParse(app.Parse(os.Args[1:]))
kingpin.FatalIfError(transformPackages(*apiGroupSuffix, *resolverFilePattern, *ignorePackageLoadErrors, *pattern...), "Failed to transform the resolver files in the specified packages.")
logger := logging.NewLogrLogger(zap.New().WithName("transformer-resolver"))
r := transformers.NewResolver(afero.NewOsFs(), *apiGroupSuffix, *ignorePackageLoadErrors, logger)
kingpin.FatalIfError(r.TransformPackages(*resolverFilePattern, *pattern...), "Failed to transform the resolver files in the specified packages.")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module github.com/crossplane/upjet

go 1.20
go 1.21

require (
dario.cat/mergo v1.0.0
Expand Down
11 changes: 11 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dario.cat/mergo v1.0.0 h1:AGCNq9Evsj31mOgNPcLyXc+4PNABt905YmuqPYYpBWk=
dario.cat/mergo v1.0.0/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1 h1:EKPd1INOIyr5hWOWhvpmQpY6tKjeG0hT1s3AMC/9fic=
github.com/AdaLogics/go-fuzz-headers v0.0.0-20230106234847-43070de90fa1/go.mod h1:VzwV+t+dZ9j/H867F1M2ziD+yLHtB46oM35FxxMJ4d0=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/agext/levenshtein v1.2.1/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
Expand Down Expand Up @@ -78,6 +79,7 @@ github.com/crossplane/crossplane v1.13.2/go.mod h1:jjYHNF5j2JidsrFZ7sfTZoVnBDVEv
github.com/crossplane/crossplane-runtime v1.14.0-rc.0.0.20231011070344-cc691421c2e5 h1:K1Km6NCu9+VlZB3CmWSjrs09cDSbwQxJd2Qw2002dFs=
github.com/crossplane/crossplane-runtime v1.14.0-rc.0.0.20231011070344-cc691421c2e5/go.mod h1:kCS5576be8g++HhiDGEBUw+8nkW8p4jhURYeC0zx8jM=
github.com/cyphar/filepath-securejoin v0.2.3 h1:YX6ebbZCZP7VkM3scTTokDgBL2TY741X51MTk3ycuNI=
github.com/cyphar/filepath-securejoin v0.2.3/go.mod h1:aPGpWjXOXUn2NCNjFvBE6aRxGGx79pTxQpKOJNYHHl4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -117,6 +119,7 @@ github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En
github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
github.com/go-test/deep v1.0.3/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/go-test/deep v1.0.7 h1:/VSMRlnY/JSyqxQUzQLKVMAskpY/NZKFA5j2P+0pP2M=
github.com/go-test/deep v1.0.7/go.mod h1:QV8Hv/iy04NyLBxAdO9njL0iVPN1S4d/A3NVv1V36o8=
Expand Down Expand Up @@ -191,6 +194,7 @@ github.com/google/pprof v0.0.0-20201023163331-3e6fc7fc9c4c/go.mod h1:kpwsk12EmLe
github.com/google/pprof v0.0.0-20201203190320-1bf35d6f28c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20201218002935-b9804c9f04c2/go.mod h1:kpwsk12EmLew5upagYY7GY0pfYCcupk39gWOCRROcvE=
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98 h1:pUa4ghanp6q4IJHwE9RwLgmVFfReJN+KbQ8ExNEUUoQ=
github.com/google/pprof v0.0.0-20230926050212-f7f687d19a98/go.mod h1:czg5+yv1E0ZGTi6S6vVK1mke0fV+FaUhNGcd6VRS9Ik=
github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
Expand Down Expand Up @@ -244,6 +248,7 @@ github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
Expand Down Expand Up @@ -289,7 +294,9 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8m
github.com/muvaf/typewriter v0.0.0-20210910160850-80e49fe1eb32 h1:yBQlHXLeUJL3TWVmzup5uT3wG5FLxhiTAiTsmNVocys=
github.com/muvaf/typewriter v0.0.0-20210910160850-80e49fe1eb32/go.mod h1:SAAdeMEiFXR8LcHffvIdiLI1w243DCH2DuHq7UrA5YQ=
github.com/onsi/ginkgo/v2 v2.11.0 h1:WgqUCUt/lT6yXoQ8Wef0fsNn5cAuMK7+KT9UFRz2tcU=
github.com/onsi/ginkgo/v2 v2.11.0/go.mod h1:ZhrRA5XmEE3x3rhlzamx/JJvujdZoJ2uvgI7kR0iZvM=
github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI=
github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M=
github.com/peterbourgon/diskv v2.0.1+incompatible h1:UBdAOUP5p4RWqPBg048CAvpKN+vxiaj6gdUUzhl4XmI=
github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -309,8 +316,10 @@ github.com/prometheus/procfs v0.10.1 h1:kYK1Va/YMlutzCGazswoHKo//tZVlFpKYh+Pymzi
github.com/prometheus/procfs v0.10.1/go.mod h1:nwNm2aOCAYw8uTR/9bWRREkZFxAUcWzPHWJq+XBB/FM=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/spf13/afero v1.10.0 h1:EaGW2JJh15aKOejeuJ+wpFSHnbd7GE6Wvp3TsNhb6LY=
github.com/spf13/afero v1.10.0/go.mod h1:UBogFpq8E9Hx+xc5CNTTEpTnuHVmXDwZcZcE1eb/UhQ=
github.com/spf13/pflag v1.0.2/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand All @@ -331,6 +340,7 @@ github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1F
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/tmccombs/hcl2json v0.3.3 h1:+DLNYqpWE0CsOQiEZu+OZm5ZBImake3wtITYxQ8uLFQ=
github.com/tmccombs/hcl2json v0.3.3/go.mod h1:Y2chtz2x9bAeRTvSibVRVgbLJhLJXKlUeIvjeVdnm4w=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down Expand Up @@ -370,6 +380,7 @@ go.starlark.net v0.0.0-20230525235612-a134d8f9ddca/go.mod h1:jxU+3+j+71eXOW14274
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.2.1 h1:NBol2c7O1ZokfZ0LEU9K6Whx/KnwvepVetCUhtKja4A=
go.uber.org/goleak v1.2.1/go.mod h1:qlT2yGI9QafXHhZZLxlSuNsMw3FFLxBr+tBRlmO1xH4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
Expand Down
164 changes: 145 additions & 19 deletions cmd/resolver/resolver.go → pkg/transformers/resolver.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
// SPDX-FileCopyrightText: 2023 The Crossplane Authors <https://crossplane.io>
// SPDX-FileCopyrightText: 2024 The Crossplane Authors <https://crossplane.io>
//
// SPDX-License-Identifier: Apache-2.0

package main
package transformers

import (
"fmt"
"go/ast"
"go/format"
"go/token"
"os"
"path/filepath"
"slices"
"strings"

"github.com/crossplane/crossplane-runtime/pkg/logging"
"github.com/pkg/errors"
"github.com/spf13/afero"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/packages"
)
Expand All @@ -23,25 +24,149 @@ const (
varManagedResource = "m"
varManagedResourceList = "l"
commentFileTransformed = "// Code transformed by upjet. DO NOT EDIT."

defaultLoadMode = packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax
)

func transformPackages(apiGroupSuffix, resolverFilePattern string, ignorePackageLoadErrors bool, patterns ...string) error {
pkgs, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedDeps | packages.NeedTypes | packages.NeedSyntax,
}, patterns...)
// Resolver transforms the source resolver implementations so that
// the resolution source managed resources are no longer statically typed
// and thus, the implementations no longer need to import the corresponding
// API packages. This transformer is helpful in preventing the import cycles
// described in https://github.com/crossplane/upjet/issues/96
// and elsewhere. Please see TransformPackages for the details of the
// transformation applied.
type Resolver struct {
// the FS implementation used for storing the transformed output
fs afero.Fs
// the API group suffix to be used for the resolution source
// managed resources, such as aws.upbound.io. Then a sample
// API group for a resource is ec2.aws.upbound.io.
apiGroupSuffix string
// When set, any errors encountered while loading the source packages is
// silently ignored if a logger is not configured,
// or logged via the configured logger.
// We need to set this when, for example, loading resolver implementations
// with import cycles, or when transforming just one package and not loading
// the referenced typed.
ignorePackageLoadErrors bool
logger logging.Logger
config *packages.Config
}

// NewResolver initializes a new Resolver with the specified configuration.
func NewResolver(fs afero.Fs, apiGroupSuffix string, ignorePackageLoadErrors bool, logger logging.Logger, opts ...ResolverOption) *Resolver {
if logger == nil {
logger = logging.NewNopLogger()
}
r := &Resolver{
fs: fs,
apiGroupSuffix: apiGroupSuffix,
ignorePackageLoadErrors: ignorePackageLoadErrors,
logger: logger,
config: &packages.Config{
Mode: defaultLoadMode,
},
}
for _, o := range opts {
o(r)
}
return r
}

// ResolverOption is an option used to configure the Resolver.
type ResolverOption func(resolver *Resolver)

// WithLoaderConfig configures the package loader config for a Resolver.
func WithLoaderConfig(c *packages.Config) ResolverOption {
return func(r *Resolver) {
r.config = c
}
}

// TransformPackages applies the dynamic resolver transformation to
// the resolver modules loaded from the specified patterns and
// implemented in the specified resolver files. If `r.ignorePackageLoadErrors`
// is set, any errors encountered while loading the source packages are
// ignored. This may be required when the transformation source files have
// compile errors, such as import cycles. The transformed resolver
// implementations will use the specified API group suffix, such as,
// "aws.upbound.io" when determining the API groups of the resolution
// source managed resources.
// A sample transformation implemented by this transformer is from:
// ```
//
// func (mg *Subnet) ResolveReferences(ctx context.Context, c client.Reader) error {
// r := reference.NewAPIResolver(c, mg)
//
// var rsp reference.ResolutionResponse
// var err error
//
// rsp, err = r.Resolve(ctx, reference.ResolutionRequest{
// CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.VPCID),
// Extract: reference.ExternalName(),
// Reference: mg.Spec.ForProvider.VPCIDRef,
// Selector: mg.Spec.ForProvider.VPCIDSelector,
// To: reference.To{
// List: &VPCList{},
// Managed: &VPC{},
// },
// })
// if err != nil {
// return errors.Wrap(err, "mg.Spec.ForProvider.VPCID")
// }
// mg.Spec.ForProvider.VPCID = reference.ToPtrValue(rsp.ResolvedValue)
// mg.Spec.ForProvider.VPCIDRef = rsp.ResolvedReference
//
// ```
// to the following:
// ```
//
// func (mg *Subnet) ResolveReferences(ctx context.Context, c client.Reader) error {
// var m xpresource.Managed
// var l xpresource.ManagedList
// r := reference.NewAPIResolver(c, mg)
//
// var rsp reference.ResolutionResponse
// var err error
// {
// m, l, err = apisresolver.GetManagedResource("ec2.aws.upbound.io", "v1beta1", "VPC", "VPCList")
// if err != nil {
// return errors.Wrap(err, "failed to get the reference target managed resource and its list for reference resolution")
// }
//
// rsp, err = r.Resolve(ctx, reference.ResolutionRequest{
// CurrentValue: reference.FromPtrValue(mg.Spec.ForProvider.VPCID),
// Extract: reference.ExternalName(),
// Reference: mg.Spec.ForProvider.VPCIDRef,
// Selector: mg.Spec.ForProvider.VPCIDSelector,
// To: reference.To{List: l, Managed: m},
// })
// }
// if err != nil {
// return errors.Wrap(err, "mg.Spec.ForProvider.VPCID")
// }
// mg.Spec.ForProvider.VPCID = reference.ToPtrValue(rsp.ResolvedValue)
// mg.Spec.ForProvider.VPCIDRef = rsp.ResolvedReference
//
// ```
func (r *Resolver) TransformPackages(resolverFilePattern string, patterns ...string) error {
pkgs, err := packages.Load(r.config, patterns...)
if err != nil {
return errors.Wrapf(err, "failed to load the packages using the patterns %q", strings.Join(patterns, ","))
}

for _, p := range pkgs {
if err := toError(p); err != nil && !ignorePackageLoadErrors {
return errors.Wrapf(err, "failed to load the package %q", p.Name)
if err := toError(p); err != nil {
if !r.ignorePackageLoadErrors {
return errors.Wrapf(err, "failed to load the package %q", p.Name)
}
r.logger.Info("Encounter the following issues when loading a package", "package", p.Name, "issues", err.Error())
}
for i, f := range p.GoFiles {
if filepath.Base(f) != resolverFilePattern {
continue
}
if err := transformResolverFile(p.Fset, p.Syntax[i], f, strings.Trim(apiGroupSuffix, ".")); err != nil {
if err := r.transformResolverFile(p.Fset, p.Syntax[i], f, strings.Trim(r.apiGroupSuffix, ".")); err != nil {
return errors.Wrapf(err, "failed to transform the resolver file %s", f)
}
}
Expand Down Expand Up @@ -100,15 +225,13 @@ func addTransformedComment(fset *token.FileSet, node *ast.File) bool {
return true
}

func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGroupSuffix string) error { //nolint:gocyclo // Arguably, easier to follow
func (r *Resolver) transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGroupSuffix string) error { //nolint:gocyclo // Arguably, easier to follow
if !addTransformedComment(fset, node) {
return nil
}
importMap, err := addMRVariableDeclarations(node)
if err != nil {
return errors.Wrapf(err, "failed to add the managed resource variable declarations to the file %s", filePath)
}

// add resolution source variable declarations to the `ResolveReferences`
// function bodies.
importMap := addMRVariableDeclarations(node)
// Map to track imports used in reference.To structs
importsUsed := make(map[string]importUsage)
// assign is the assignment statement that assigns the values returned from
Expand Down Expand Up @@ -325,10 +448,13 @@ func transformResolverFile(fset *token.FileSet, node *ast.File, filePath, apiGro
}
}
}
return r.dumpTransformed(fset, node, filePath)
}

func (r *Resolver) dumpTransformed(fset *token.FileSet, node *ast.File, filePath string) error {
// dump the transformed resolver file
adjustFunctionDocs(node)
outFile, err := os.Create(filepath.Clean(filePath))
outFile, err := r.fs.Create(filepath.Clean(filePath))
if err != nil {
return errors.Wrap(err, "failed to open the resolver file for writing the transformed AST")
}
Expand Down Expand Up @@ -356,7 +482,7 @@ func insertStatements(stmts []ast.Stmt, block *ast.BlockStmt, assign *ast.Assign
return true
}

func addMRVariableDeclarations(f *ast.File) (map[string]string, error) { //nolint:gocyclo
func addMRVariableDeclarations(f *ast.File) map[string]string {
// prepare the first variable declaration:
// `var m xpresource.Managed`
varDecl1 := &ast.GenDecl{
Expand Down Expand Up @@ -403,7 +529,7 @@ func addMRVariableDeclarations(f *ast.File) (map[string]string, error) { //nolin
})
return map[string]string{
`"github.com/crossplane/crossplane-runtime/pkg/resource"`: "xpresource",
}, nil
}
}

func getManagedResourceStatements(group, version, kind, listKind string) (map[string]string, []ast.Stmt) {
Expand Down
Loading

0 comments on commit 1493017

Please sign in to comment.