Unpointerize some types
In a few places, an IDL type will be a pointer, but the resulting RPC code would, ideally, be the naked type. Namely, in both resource and asset cases, they are required to be pointers in the IDL (because they are by-pointer by nature), but the marshaled representations need not be pointers. This change depointerizes such types in the RPC unless, of course, they are optional in which case pointers still make sense. This avoids some annoying dereferencing and is the kind of thing we want to do sooner before seeing widespread use.
This commit is contained in:
parent
fe93f5e76f
commit
fa24d436e3
29 changed files with 69 additions and 54 deletions
|
@ -114,7 +114,7 @@ func (p *funcProvider) Create(ctx context.Context, obj *lambda.Function) (resour
|
|||
}
|
||||
} else {
|
||||
// TODO: assets need filenames; don't hard code it.
|
||||
if zip, err := zipCodeAsset(*obj.Code, "index.js"); err != nil {
|
||||
if zip, err := zipCodeAsset(obj.Code, "index.js"); err != nil {
|
||||
return "", nil, err
|
||||
} else {
|
||||
code = &awslambda.FunctionCode{ZipFile: zip}
|
||||
|
|
|
@ -173,7 +173,7 @@ func (p *BasePathMappingProvider) Unmarshal(
|
|||
type BasePathMapping struct {
|
||||
Name string `json:"name"`
|
||||
DomainName string `json:"domainName"`
|
||||
RestAPI *resource.ID `json:"restAPI"`
|
||||
RestAPI resource.ID `json:"restAPI"`
|
||||
BasePath *string `json:"basePath,omitempty"`
|
||||
Stage *resource.ID `json:"stage,omitempty"`
|
||||
}
|
||||
|
|
|
@ -172,7 +172,7 @@ func (p *DeploymentProvider) Unmarshal(
|
|||
// Deployment is a marshalable representation of its corresponding IDL type.
|
||||
type Deployment struct {
|
||||
Name string `json:"name"`
|
||||
RestAPI *resource.ID `json:"restAPI"`
|
||||
RestAPI resource.ID `json:"restAPI"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
StageDescription *StageDescription `json:"stageDescription,omitempty"`
|
||||
StageName *string `json:"stageName,omitempty"`
|
||||
|
|
|
@ -221,14 +221,14 @@ func (p *MethodProvider) Unmarshal(
|
|||
type Method struct {
|
||||
Name string `json:"name"`
|
||||
HTTPMethod string `json:"httpMethod"`
|
||||
APIResource *resource.ID `json:"apiResource"`
|
||||
RestAPI *resource.ID `json:"restAPI"`
|
||||
APIResource resource.ID `json:"apiResource"`
|
||||
RestAPI resource.ID `json:"restAPI"`
|
||||
APIKeyRequired *bool `json:"apiKeyRequired,omitempty"`
|
||||
AuthorizationType *AuthorizationType `json:"authorizationType,omitempty"`
|
||||
Authorizer *resource.ID `json:"authorizer,omitempty"`
|
||||
Integration *Integration `json:"integration,omitempty"`
|
||||
MethodResponses *[]MethodResponse `json:"methodResponses,omitempty"`
|
||||
RequestModels *map[string]*resource.ID `json:"requestModels,omitempty"`
|
||||
RequestModels *map[string]resource.ID `json:"requestModels,omitempty"`
|
||||
RequestParameters *map[string]bool `json:"requestParameters,omitempty"`
|
||||
}
|
||||
|
||||
|
@ -252,7 +252,7 @@ const (
|
|||
// MethodResponse is a marshalable representation of its corresponding IDL type.
|
||||
type MethodResponse struct {
|
||||
StatusCode string `json:"statusCode"`
|
||||
ResponseModels *map[string]*resource.ID `json:"responseModels,omitempty"`
|
||||
ResponseModels *map[string]resource.ID `json:"responseModels,omitempty"`
|
||||
ResponseParameters *map[string]bool `json:"responseParameters,omitempty"`
|
||||
}
|
||||
|
||||
|
|
|
@ -182,7 +182,7 @@ func (p *ModelProvider) Unmarshal(
|
|||
type Model struct {
|
||||
Name string `json:"name"`
|
||||
ContentType string `json:"contentType"`
|
||||
RestAPI *resource.ID `json:"restAPI"`
|
||||
RestAPI resource.ID `json:"restAPI"`
|
||||
Schema interface{} `json:"schema"`
|
||||
ModelName *string `json:"modelName,omitempty"`
|
||||
Description *string `json:"description,omitempty"`
|
||||
|
|
|
@ -181,9 +181,9 @@ func (p *ResourceProvider) Unmarshal(
|
|||
// Resource is a marshalable representation of its corresponding IDL type.
|
||||
type Resource struct {
|
||||
Name string `json:"name"`
|
||||
Parent *resource.ID `json:"parent"`
|
||||
Parent resource.ID `json:"parent"`
|
||||
PathPart string `json:"pathPart"`
|
||||
RestAPI *resource.ID `json:"restAPI"`
|
||||
RestAPI resource.ID `json:"restAPI"`
|
||||
}
|
||||
|
||||
// Resource's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -197,7 +197,7 @@ const (
|
|||
|
||||
// S3Location is a marshalable representation of its corresponding IDL type.
|
||||
type S3Location struct {
|
||||
Object *resource.ID `json:"object"`
|
||||
Object resource.ID `json:"object"`
|
||||
ETag *string `json:"etag,omitempty"`
|
||||
Version *string `json:"version,omitempty"`
|
||||
}
|
||||
|
|
|
@ -178,9 +178,9 @@ func (p *StageProvider) Unmarshal(
|
|||
// Stage is a marshalable representation of its corresponding IDL type.
|
||||
type Stage struct {
|
||||
Name string `json:"name"`
|
||||
RestAPI *resource.ID `json:"restAPI"`
|
||||
RestAPI resource.ID `json:"restAPI"`
|
||||
StageName string `json:"stageName"`
|
||||
Deployment *resource.ID `json:"deployment"`
|
||||
Deployment resource.ID `json:"deployment"`
|
||||
CacheClusterEnabled *bool `json:"cacheClusterEnabled,omitempty"`
|
||||
CacheClusterSize *string `json:"cacheClusterSize,omitempty"`
|
||||
ClientCertificate *resource.ID `json:"clientCertificate,omitempty"`
|
||||
|
|
|
@ -178,8 +178,8 @@ func (p *UsagePlanKeyProvider) Unmarshal(
|
|||
// UsagePlanKey is a marshalable representation of its corresponding IDL type.
|
||||
type UsagePlanKey struct {
|
||||
Name string `json:"name"`
|
||||
Key *resource.ID `json:"key"`
|
||||
UsagePlan *resource.ID `json:"usagePlan"`
|
||||
Key resource.ID `json:"key"`
|
||||
UsagePlan resource.ID `json:"usagePlan"`
|
||||
}
|
||||
|
||||
// UsagePlanKey's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -177,7 +177,7 @@ type Instance struct {
|
|||
Name string `json:"name"`
|
||||
ImageID string `json:"imageId"`
|
||||
InstanceType *InstanceType `json:"instanceType,omitempty"`
|
||||
SecurityGroups *[]*resource.ID `json:"securityGroups,omitempty"`
|
||||
SecurityGroups *[]resource.ID `json:"securityGroups,omitempty"`
|
||||
KeyName *string `json:"keyName,omitempty"`
|
||||
AvailabilityZone string `json:"availabilityZone,omitempty"`
|
||||
PrivateDNSName *string `json:"privateDNSName,omitempty"`
|
||||
|
|
|
@ -185,9 +185,9 @@ func (p *RouteProvider) Unmarshal(
|
|||
type Route struct {
|
||||
Name string `json:"name"`
|
||||
DestinationCidrBlock string `json:"destinationCidrBlock"`
|
||||
RouteTable *resource.ID `json:"routeTable"`
|
||||
InternetGateway *resource.ID `json:"internetGateway"`
|
||||
VPCGatewayAttachment *resource.ID `json:"vpcGatewayAttachment"`
|
||||
RouteTable resource.ID `json:"routeTable"`
|
||||
InternetGateway resource.ID `json:"internetGateway"`
|
||||
VPCGatewayAttachment resource.ID `json:"vpcGatewayAttachment"`
|
||||
}
|
||||
|
||||
// Route's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -175,7 +175,7 @@ func (p *RouteTableProvider) Unmarshal(
|
|||
// RouteTable is a marshalable representation of its corresponding IDL type.
|
||||
type RouteTable struct {
|
||||
Name string `json:"name"`
|
||||
VPC *resource.ID `json:"vpc"`
|
||||
VPC resource.ID `json:"vpc"`
|
||||
}
|
||||
|
||||
// RouteTable's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -197,7 +197,7 @@ func (p *SecurityGroupEgressProvider) Unmarshal(
|
|||
type SecurityGroupEgress struct {
|
||||
Name string `json:"name"`
|
||||
FromPort float64 `json:"fromPort"`
|
||||
Group *resource.ID `json:"group"`
|
||||
Group resource.ID `json:"group"`
|
||||
IPProtocol string `json:"ipProtocol"`
|
||||
ToPort float64 `json:"toPort"`
|
||||
CIDRIP *string `json:"cidrIp,omitempty"`
|
||||
|
|
|
@ -182,7 +182,7 @@ func (p *SubnetProvider) Unmarshal(
|
|||
type Subnet struct {
|
||||
Name string `json:"name"`
|
||||
CIDRBlock string `json:"cidrBlock"`
|
||||
VPC *resource.ID `json:"vpc"`
|
||||
VPC resource.ID `json:"vpc"`
|
||||
AvailabilityZone *string `json:"availabilityZone,omitempty"`
|
||||
MapPublicIpOnLaunch *bool `json:"mapPublicIpOnLaunch,omitempty"`
|
||||
}
|
||||
|
|
|
@ -178,8 +178,8 @@ func (p *VPCGatewayAttachmentProvider) Unmarshal(
|
|||
// VPCGatewayAttachment is a marshalable representation of its corresponding IDL type.
|
||||
type VPCGatewayAttachment struct {
|
||||
Name string `json:"name"`
|
||||
VPC *resource.ID `json:"vpc"`
|
||||
InternetGateway *resource.ID `json:"internetGateway"`
|
||||
VPC resource.ID `json:"vpc"`
|
||||
InternetGateway resource.ID `json:"internetGateway"`
|
||||
}
|
||||
|
||||
// VPCGatewayAttachment's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -178,8 +178,8 @@ func (p *VPCPeeringConnectionProvider) Unmarshal(
|
|||
// VPCPeeringConnection is a marshalable representation of its corresponding IDL type.
|
||||
type VPCPeeringConnection struct {
|
||||
Name string `json:"name"`
|
||||
PeerVPC *resource.ID `json:"peerVpc"`
|
||||
VPC *resource.ID `json:"vpc"`
|
||||
PeerVPC resource.ID `json:"peerVpc"`
|
||||
VPC resource.ID `json:"vpc"`
|
||||
}
|
||||
|
||||
// VPCPeeringConnection's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -176,7 +176,7 @@ func (p *GroupProvider) Unmarshal(
|
|||
type Group struct {
|
||||
Name string `json:"name"`
|
||||
GroupName *string `json:"groupName,omitempty"`
|
||||
ManagedPolicies *[]*resource.ID `json:"managedPolicies,omitempty"`
|
||||
ManagedPolicies *[]resource.ID `json:"managedPolicies,omitempty"`
|
||||
Path *string `json:"path,omitempty"`
|
||||
Policies *InlinePolicy `json:"policies,omitempty"`
|
||||
}
|
||||
|
|
|
@ -188,9 +188,9 @@ type Policy struct {
|
|||
Name string `json:"name"`
|
||||
PolicyDocument interface{} `json:"policyDocument"`
|
||||
PolicyName string `json:"policyName"`
|
||||
Groups *[]*resource.ID `json:"groups,omitempty"`
|
||||
Roles *[]*resource.ID `json:"roles,omitempty"`
|
||||
Users *[]*resource.ID `json:"users,omitempty"`
|
||||
Groups *[]resource.ID `json:"groups,omitempty"`
|
||||
Roles *[]resource.ID `json:"roles,omitempty"`
|
||||
Users *[]resource.ID `json:"users,omitempty"`
|
||||
}
|
||||
|
||||
// Policy's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -190,9 +190,9 @@ func (p *UserProvider) Unmarshal(
|
|||
type User struct {
|
||||
Name string `json:"name"`
|
||||
UserName *string `json:"userName,omitempty"`
|
||||
Groups *[]*resource.ID `json:"groups,omitempty"`
|
||||
Groups *[]resource.ID `json:"groups,omitempty"`
|
||||
LoginProfile *LoginProfile `json:"loginProfile,omitempty"`
|
||||
ManagedPolicies *[]*resource.ID `json:"managedPolicies,omitempty"`
|
||||
ManagedPolicies *[]resource.ID `json:"managedPolicies,omitempty"`
|
||||
Path *string `json:"path,omitempty"`
|
||||
Policies *[]InlinePolicy `json:"policies,omitempty"`
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ import (
|
|||
|
||||
// DeadLetterConfig is a marshalable representation of its corresponding IDL type.
|
||||
type DeadLetterConfig struct {
|
||||
Target *resource.ID `json:"target"`
|
||||
Target resource.ID `json:"target"`
|
||||
}
|
||||
|
||||
// DeadLetterConfig's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
@ -189,9 +189,9 @@ func (p *FunctionProvider) Unmarshal(
|
|||
// Function is a marshalable representation of its corresponding IDL type.
|
||||
type Function struct {
|
||||
Name string `json:"name"`
|
||||
Code *resource.Asset `json:"code"`
|
||||
Code resource.Asset `json:"code"`
|
||||
Handler string `json:"handler"`
|
||||
Role *resource.ID `json:"role"`
|
||||
Role resource.ID `json:"role"`
|
||||
Runtime Runtime `json:"runtime"`
|
||||
FunctionName *string `json:"functionName,omitempty"`
|
||||
DeadLetterConfig *DeadLetterConfig `json:"deadLetterConfig,omitempty"`
|
||||
|
@ -231,8 +231,8 @@ const (
|
|||
|
||||
// VPCConfig is a marshalable representation of its corresponding IDL type.
|
||||
type VPCConfig struct {
|
||||
SecurityGroups []*resource.ID `json:"securityGroups"`
|
||||
Subnets []*resource.ID `json:"subnets"`
|
||||
SecurityGroups []resource.ID `json:"securityGroups"`
|
||||
Subnets []resource.ID `json:"subnets"`
|
||||
}
|
||||
|
||||
// VPCConfig's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -190,7 +190,7 @@ func (p *PermissionProvider) Unmarshal(
|
|||
type Permission struct {
|
||||
Name string `json:"name"`
|
||||
Action string `json:"action"`
|
||||
Function *resource.ID `json:"function"`
|
||||
Function resource.ID `json:"function"`
|
||||
Principal string `json:"principal"`
|
||||
SourceAccount *string `json:"sourceAccount,omitempty"`
|
||||
SourceARN *__aws.ARN `json:"sourceARN,omitempty"`
|
||||
|
|
|
@ -175,8 +175,8 @@ func (p *ObjectProvider) Unmarshal(
|
|||
// Object is a marshalable representation of its corresponding IDL type.
|
||||
type Object struct {
|
||||
Key string `json:"key"`
|
||||
Bucket *resource.ID `json:"bucket"`
|
||||
Source *resource.Asset `json:"source"`
|
||||
Bucket resource.ID `json:"bucket"`
|
||||
Source resource.Asset `json:"source"`
|
||||
}
|
||||
|
||||
// Object's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -207,7 +207,7 @@ const (
|
|||
|
||||
// RedrivePolicy is a marshalable representation of its corresponding IDL type.
|
||||
type RedrivePolicy struct {
|
||||
DeadLetterTarget *resource.ID `json:"deadLetterTarget"`
|
||||
DeadLetterTarget resource.ID `json:"deadLetterTarget"`
|
||||
MaxReceiveCount float64 `json:"maxReceiveCount"`
|
||||
}
|
||||
|
||||
|
|
|
@ -51,7 +51,7 @@ func (p *funcProvider) Create(ctx context.Context, obj *idl.Function) (resource.
|
|||
Name: obj.Name,
|
||||
},
|
||||
Environment: fission.Metadata{
|
||||
Name: string(*obj.Environment),
|
||||
Name: string(obj.Environment),
|
||||
},
|
||||
Code: code.String(),
|
||||
}
|
||||
|
|
|
@ -41,7 +41,7 @@ func (p *httProvider) Create(ctx context.Context, obj *idl.HTTPTrigger) (resourc
|
|||
Metadata: fission.Metadata{Name: obj.Name},
|
||||
UrlPattern: obj.URLPattern,
|
||||
Method: obj.Method,
|
||||
Function: fission.Metadata{Name: string(*obj.Function)},
|
||||
Function: fission.Metadata{Name: string(obj.Function)},
|
||||
}); err != nil {
|
||||
return "", err
|
||||
} else {
|
||||
|
|
|
@ -172,8 +172,8 @@ func (p *FunctionProvider) Unmarshal(
|
|||
// Function is a marshalable representation of its corresponding IDL type.
|
||||
type Function struct {
|
||||
Name string `json:"name"`
|
||||
Environment *resource.ID `json:"environment"`
|
||||
Code *resource.Asset `json:"code"`
|
||||
Environment resource.ID `json:"environment"`
|
||||
Code resource.Asset `json:"code"`
|
||||
}
|
||||
|
||||
// Function's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -174,7 +174,7 @@ type HTTPTrigger struct {
|
|||
Name string `json:"name"`
|
||||
URLPattern string `json:"urlPattern"`
|
||||
Method string `json:"method"`
|
||||
Function *resource.ID `json:"function"`
|
||||
Function resource.ID `json:"function"`
|
||||
}
|
||||
|
||||
// HTTPTrigger's properties have constants to make dealing with diffs and property bags easier.
|
||||
|
|
|
@ -176,7 +176,7 @@ type Watch struct {
|
|||
ObjType string `json:"objType"`
|
||||
LabelSelector string `json:"labelSelector"`
|
||||
FieldSelector string `json:"fieldSelector"`
|
||||
Function *resource.ID `json:"function"`
|
||||
Function resource.ID `json:"function"`
|
||||
Target string `json:"target"`
|
||||
}
|
||||
|
||||
|
|
|
@ -426,7 +426,7 @@ func (g *RPCGenerator) EmitStructType(w *bufio.Writer, module tokens.Module, pkg
|
|||
}
|
||||
// Make a JSON tag for this so we can serialize; note that outputs are always optional in this position.
|
||||
jsontag := makeJSONTag(opts, opts.Out)
|
||||
writefmtln(w, " %v %v %v", prop.Name(), g.GenTypeName(prop.Type()), jsontag)
|
||||
writefmtln(w, " %v %v %v", prop.Name(), g.GenTypeName(prop.Type(), opts.Optional), jsontag)
|
||||
}
|
||||
writefmtln(w, "}")
|
||||
writefmtln(w, "")
|
||||
|
@ -438,7 +438,7 @@ func (g *RPCGenerator) EmitStructType(w *bufio.Writer, module tokens.Module, pkg
|
|||
prop := props[out]
|
||||
opts := propopts[out]
|
||||
jsontag := makeJSONTag(opts, false)
|
||||
writefmtln(w, " %v %v %v", prop.Name(), g.GenTypeName(prop.Type()), jsontag)
|
||||
writefmtln(w, " %v %v %v", prop.Name(), g.GenTypeName(prop.Type(), opts.Optional), jsontag)
|
||||
}
|
||||
writefmtln(w, "}")
|
||||
writefmtln(w, "")
|
||||
|
@ -465,7 +465,7 @@ func makeJSONTag(opts PropertyOptions, forceopt bool) string {
|
|||
return fmt.Sprintf("`json:\"%v%v\"`", opts.Name, flags)
|
||||
}
|
||||
|
||||
func (g *RPCGenerator) GenTypeName(t types.Type) string {
|
||||
func (g *RPCGenerator) GenTypeName(t types.Type, opt bool) string {
|
||||
switch u := t.(type) {
|
||||
case *types.Basic:
|
||||
switch k := u.Kind(); k {
|
||||
|
@ -510,11 +510,26 @@ func (g *RPCGenerator) GenTypeName(t types.Type) string {
|
|||
impname := g.registerImport(pkg)
|
||||
return fmt.Sprintf("%v.%v", impname, name)
|
||||
case *types.Map:
|
||||
return fmt.Sprintf("map[%v]%v", g.GenTypeName(u.Key()), g.GenTypeName(u.Elem()))
|
||||
return fmt.Sprintf("map[%v]%v", g.GenTypeName(u.Key(), false), g.GenTypeName(u.Elem(), false))
|
||||
case *types.Pointer:
|
||||
return fmt.Sprintf("*%v", g.GenTypeName(u.Elem()))
|
||||
// If this isn't an optional property, and the underlying type is a resource or special type, unpointerize it.
|
||||
elem := u.Elem()
|
||||
unptr := false
|
||||
if !opt {
|
||||
if elnm, iselnm := elem.(*types.Named); iselnm {
|
||||
if res, _ := IsResource(elnm.Obj(), elnm); res {
|
||||
unptr = true
|
||||
} else if spec, _ := IsSpecial(elnm.Obj()); spec {
|
||||
unptr = true
|
||||
}
|
||||
}
|
||||
}
|
||||
if unptr {
|
||||
return g.GenTypeName(elem, false)
|
||||
}
|
||||
return fmt.Sprintf("*%v", g.GenTypeName(u.Elem(), false))
|
||||
case *types.Slice:
|
||||
return fmt.Sprintf("[]%v", g.GenTypeName(u.Elem())) // postfix syntax for arrays.
|
||||
return fmt.Sprintf("[]%v", g.GenTypeName(u.Elem(), false)) // postfix syntax for arrays.
|
||||
default:
|
||||
contract.Failf("Unrecognized GenTypeName type: %v", reflect.TypeOf(u))
|
||||
}
|
||||
|
@ -554,7 +569,7 @@ func (g *RPCGenerator) EmitConstants(w *bufio.Writer, consts []*Const) {
|
|||
|
||||
writefmtln(w, "const (")
|
||||
for _, konst := range consts {
|
||||
writefmtln(w, " %v %v = %v", konst.Name(), g.GenTypeName(konst.Type), konst.Value)
|
||||
writefmtln(w, " %v %v = %v", konst.Name(), g.GenTypeName(konst.Type, false), konst.Value)
|
||||
}
|
||||
writefmtln(w, ")")
|
||||
|
||||
|
|
Loading…
Reference in a new issue