Skip to content

Commit

Permalink
centralize getToken logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Sep 26, 2023
1 parent 3f7a4fd commit 7071a97
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 69 deletions.
9 changes: 1 addition & 8 deletions infer/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package infer

import (
"fmt"
"reflect"

pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
Expand Down Expand Up @@ -75,13 +74,7 @@ func (rc *derivedComponentController[R, I, O]) GetSchema(reg schema.RegisterDeri
}

func (rc *derivedComponentController[R, I, O]) GetToken() (tokens.Type, error) {
var r R
annotator := getAnnotated(reflect.TypeOf(r))
if annotator.Token != "" {
return fnToken(tokens.Type(annotator.Token)), nil
}

return introspect.GetToken("pkg", r)
return getToken[R](nil)
}

func (rc *derivedComponentController[R, I, O]) Construct(
Expand Down
22 changes: 10 additions & 12 deletions infer/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (

p "github.com/pulumi/pulumi-go-provider"
"github.com/pulumi/pulumi-go-provider/infer/internal/ende"
"github.com/pulumi/pulumi-go-provider/internal/introspect"
t "github.com/pulumi/pulumi-go-provider/middleware"
"github.com/pulumi/pulumi-go-provider/middleware/schema"
)
Expand Down Expand Up @@ -53,17 +52,16 @@ type derivedInvokeController[F Fn[I, O], I, O any] struct{}

func (derivedInvokeController[F, I, O]) isInferredFunction() {}

func (c *derivedInvokeController[F, I, O]) GetToken() (tokens.Type, error) {
var f F
annotator := getAnnotated(reflect.TypeOf(f))
if annotator.Token != "" {
return fnToken(tokens.Type(annotator.Token)), nil
}
tk, err := introspect.GetToken("pkg", f)
if err != nil {
return "", err
}
return fnToken(tk), nil
func (*derivedInvokeController[F, I, O]) GetToken() (tokens.Type, error) {
// By default, we get resource style tokens:
//
// pkg:index:FizzBuzz
//
// Functions use a different capitalization convection, so we need to convert:
//
// pkg:index:FizzBuzz
//
return getToken[F](fnToken)
}

func fnToken(tk tokens.Type) tokens.Type {
Expand Down
22 changes: 18 additions & 4 deletions infer/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package infer

import (
"fmt"
"reflect"

"github.com/hashicorp/go-multierror"
pschema "github.com/pulumi/pulumi/pkg/v3/codegen/schema"
Expand Down Expand Up @@ -682,14 +683,27 @@ func (*derivedResourceController[R, I, O]) GetSchema(reg schema.RegisterDerivati
return r, errs.ErrorOrNil()
}

func (*derivedResourceController[R, I, O]) GetToken() (tokens.Type, error) {
func getToken[R any](transform func(tokens.Type) tokens.Type) (tokens.Type, error) {
var r R
annotator := getAnnotated(reflect.TypeOf(r))
return getTokenOf(reflect.TypeOf(r), transform)
}

func getTokenOf(t reflect.Type, transform func(tokens.Type) tokens.Type) (tokens.Type, error) {
annotator := getAnnotated(t)
if annotator.Token != "" {
return fnToken(tokens.Type(annotator.Token)), nil
return tokens.Type(annotator.Token), nil
}

tk, err := introspect.GetToken("pkg", t)
if transform == nil || err != nil {
return tk, err
}

return introspect.GetToken("pkg", r)
return transform(tk), nil
}

func (*derivedResourceController[R, I, O]) GetToken() (tokens.Type, error) {
return getToken[R](nil)
}

func (*derivedResourceController[R, I, O]) getInstance() *R {
Expand Down
14 changes: 3 additions & 11 deletions infer/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/hashicorp/go-multierror"
"github.com/pulumi/pulumi/pkg/v3/codegen/schema"
"github.com/pulumi/pulumi/sdk/v3/go/common/resource"
"github.com/pulumi/pulumi/sdk/v3/go/common/tokens"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"

"github.com/pulumi/pulumi-go-provider/internal/introspect"
Expand Down Expand Up @@ -336,16 +335,9 @@ func structReferenceToken(t reflect.Type, extTag *introspect.ExplicitType) (sche
return schema.TypeSpec{}, false, nil
}

var tk tokens.Type
annotator := getAnnotated(t)
if annotator.Token != "" {
tk = fnToken(tokens.Type(annotator.Token))
} else {
var err error
tk, err = introspect.GetToken("pkg", reflect.New(t).Interface())
if err != nil {
return schema.TypeSpec{}, true, err
}
tk, err := getTokenOf(t, nil)
if err != nil {
return schema.TypeSpec{}, true, err
}

return schema.TypeSpec{
Expand Down
23 changes: 5 additions & 18 deletions infer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,8 @@ func isEnum(t reflect.Type) (enum, bool) {
}
}

var tk tokens.Type
annotator := getAnnotated(t)
if annotator.Token != "" {
tk = fnToken(tokens.Type(annotator.Token))
} else {
var err error
tk, err = introspect.GetToken("pkg", reflect.New(t).Elem().Interface())
contract.AssertNoErrorf(err, "failed to get token for enum: %s", t)
}
tk, err := getTokenOf(t, nil)
contract.AssertNoErrorf(err, "failed to get token for enum: %s", t)

return enum{
token: tk.String(),
Expand Down Expand Up @@ -254,15 +247,9 @@ func registerTypes[T any](reg schema.RegisterDerivativeType) error {
return false, err
}

var tk tokens.Type
annotator := getAnnotated(t)
if annotator.Token != "" {
tk = fnToken(tokens.Type(annotator.Token))
} else {
tk, err = introspect.GetToken("pkg", reflect.New(t).Interface())
if err != nil {
return false, err
}
tk, err := getTokenOf(t, nil)
if err != nil {
return false, err
}

return reg(tk, pschema.ComplexTypeSpec{ObjectTypeSpec: *spec}), nil
Expand Down
21 changes: 5 additions & 16 deletions internal/introspect/introspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ package introspect
import (
"fmt"
"reflect"
"runtime"
"strings"

"github.com/blang/semver"
Expand Down Expand Up @@ -79,8 +78,7 @@ func FindProperties(r any) (map[string]FieldTag, error) {
}

// Get the token that represents a struct.
func GetToken(pkg tokens.Package, i any) (tokens.Type, error) {
typ := reflect.TypeOf(i)
func GetToken(pkg tokens.Package, typ reflect.Type) (tokens.Type, error) {
if typ == nil {
return "", fmt.Errorf("cannot get token of nil type")
}
Expand All @@ -89,23 +87,14 @@ func GetToken(pkg tokens.Package, i any) (tokens.Type, error) {
typ = typ.Elem()
}

var name string
var mod string
if typ.Kind() == reflect.Func {
fn := runtime.FuncForPC(reflect.ValueOf(i).Pointer())
parts := strings.Split(fn.Name(), ".")
name = parts[len(parts)-1]
mod = strings.Join(parts[:len(parts)-1], "/")
} else {
name = typ.Name()
mod = strings.Trim(typ.PkgPath(), "*")
}
name := typ.Name()
mod := strings.Trim(typ.PkgPath(), "*")

if name == "" {
return "", fmt.Errorf("type %T has no name", i)
return "", fmt.Errorf("type %s has no name", typ)
}
if mod == "" {
return "", fmt.Errorf("type %T has no module path", i)
return "", fmt.Errorf("type %s has no module path", typ)
}
// Take off the pkg name, since that is supplied by `pkg`.
mod = mod[strings.LastIndex(mod, "/")+1:]
Expand Down

0 comments on commit 7071a97

Please sign in to comment.