Skip to content

Commit 00e63b0

Browse files
committed
found a way to fix the apigateway types conflict with gob
1 parent eaac503 commit 00e63b0

2 files changed

Lines changed: 85 additions & 7 deletions

File tree

aws/sdk/apigateway.go

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,32 @@ type APIGatewayClientInterface interface {
2323
GetUsagePlanKeys(context.Context, *apigateway.GetUsagePlanKeysInput, ...func(*apigateway.Options)) (*apigateway.GetUsagePlanKeysOutput, error)
2424
}
2525

26+
type CachedGetMethodOutput struct {
27+
ApiKeyRequired *bool
28+
AuthorizationScopes []string
29+
AuthorizationType *string
30+
AuthorizerId *string
31+
HttpMethod *string
32+
MethodIntegration *apiGatewayTypes.Integration
33+
MethodResponses map[string]apiGatewayTypes.MethodResponse
34+
OperationName *string
35+
RequestModels map[string]string
36+
RequestParameters map[string]bool
37+
RequestValidatorId *string
38+
ResourceId *string
39+
}
40+
41+
type CachedGetStagesOutput struct {
42+
Item []apiGatewayTypes.Stage
43+
}
44+
2645
func init() {
2746
gob.Register([]apiGatewayTypes.RestApi{})
28-
gob.Register(apigateway.GetStagesOutput{})
47+
gob.Register(CachedGetStagesOutput{})
2948
gob.Register([]apiGatewayTypes.Resource{})
3049
gob.Register([]apiGatewayTypes.DomainName{})
3150
gob.Register([]apiGatewayTypes.BasePathMapping{})
32-
gob.Register(apigateway.GetMethodOutput{})
51+
gob.Register(CachedGetMethodOutput{})
3352
gob.Register([]apiGatewayTypes.UsagePlan{})
3453
gob.Register([]apiGatewayTypes.UsagePlanKey{})
3554
}
@@ -76,7 +95,9 @@ func CachedApiGatewayGetStages(client APIGatewayClientInterface, accountID strin
7695
cacheKey := fmt.Sprintf("%s-apigateway-GetStages-%s-%s", accountID, region, restAPIID)
7796
cached, found := internal.Cache.Get(cacheKey)
7897
if found {
79-
return cached.(*apigateway.GetStagesOutput), nil
98+
// Convert cached data back to GetStagesOutput before returning
99+
cachedOutput := cached.(*CachedGetStagesOutput) // Ensure this type assertion matches your caching logic
100+
return fromCachedGetStagesOutput(cachedOutput), nil
80101
}
81102

82103
GetStages, err := client.GetStages(
@@ -93,10 +114,27 @@ func CachedApiGatewayGetStages(client APIGatewayClientInterface, accountID strin
93114
return &apigateway.GetStagesOutput{}, err
94115
}
95116

96-
internal.Cache.Set(cacheKey, GetStages, cache.DefaultExpiration)
117+
// Convert GetStagesOutput to CachedGetStagesOutput before caching
118+
cachedVersion := toCachedGetStagesOutput(GetStages)
119+
internal.Cache.Set(cacheKey, cachedVersion, cache.DefaultExpiration)
97120
return GetStages, err
98121
}
99122

123+
// Convert from AWS SDK type to custom type for caching
124+
func toCachedGetStagesOutput(gso *apigateway.GetStagesOutput) *CachedGetStagesOutput {
125+
return &CachedGetStagesOutput{
126+
Item: gso.Item,
127+
}
128+
}
129+
130+
// Convert back to AWS SDK type after fetching from cache
131+
func fromCachedGetStagesOutput(cgso *CachedGetStagesOutput) *apigateway.GetStagesOutput {
132+
return &apigateway.GetStagesOutput{
133+
Item: cgso.Item,
134+
// Initialize ResultMetadata or leave it as zero value if it's not required for your use case
135+
}
136+
}
137+
100138
// create a CachedApiGatewayGetResources function that accepts a client, account id, region, and rest api id. Make sure it handles caching, the region option and pagination
101139
func CachedApiGatewayGetResources(client APIGatewayClientInterface, accountID string, region string, restAPIID string) ([]apiGatewayTypes.Resource, error) {
102140
var PaginationControl *string
@@ -216,7 +254,9 @@ func CachedApiGatewayGetMethod(client APIGatewayClientInterface, accountID strin
216254
cacheKey := fmt.Sprintf("%s-apigateway-GetMethod-%s-%s-%s-%s", accountID, region, restAPIID, resourceID, method)
217255
cached, found := internal.Cache.Get(cacheKey)
218256
if found {
219-
return cached.(*apigateway.GetMethodOutput), nil
257+
// Convert cached data back to GetMethodOutput before returning
258+
cachedOutput := cached.(*CachedGetMethodOutput) // Ensure this type assertion matches your caching logic
259+
return fromCachedGetMethodOutput(cachedOutput), nil
220260
}
221261

222262
GetMethod, err := client.GetMethod(
@@ -235,11 +275,48 @@ func CachedApiGatewayGetMethod(client APIGatewayClientInterface, accountID strin
235275
return &apigateway.GetMethodOutput{}, err
236276
}
237277

238-
internal.Cache.Set(cacheKey, GetMethod, cache.DefaultExpiration)
278+
// Convert GetMethodOutput to CachedGetMethodOutput before caching
279+
cachedVersion := toCachedGetMethodOutput(GetMethod)
280+
internal.Cache.Set(cacheKey, cachedVersion, cache.DefaultExpiration)
239281
return GetMethod, nil
240282

241283
}
242284

285+
// Convert from AWS SDK type to custom type for caching
286+
func toCachedGetMethodOutput(gmo *apigateway.GetMethodOutput) *CachedGetMethodOutput {
287+
return &CachedGetMethodOutput{
288+
ApiKeyRequired: gmo.ApiKeyRequired,
289+
AuthorizationScopes: gmo.AuthorizationScopes,
290+
AuthorizationType: gmo.AuthorizationType,
291+
AuthorizerId: gmo.AuthorizerId,
292+
HttpMethod: gmo.HttpMethod,
293+
MethodIntegration: gmo.MethodIntegration,
294+
MethodResponses: gmo.MethodResponses,
295+
OperationName: gmo.OperationName,
296+
RequestModels: gmo.RequestModels,
297+
RequestParameters: gmo.RequestParameters,
298+
RequestValidatorId: gmo.RequestValidatorId,
299+
}
300+
}
301+
302+
// Convert back to AWS SDK type after fetching from cache
303+
func fromCachedGetMethodOutput(cgmo *CachedGetMethodOutput) *apigateway.GetMethodOutput {
304+
return &apigateway.GetMethodOutput{
305+
ApiKeyRequired: cgmo.ApiKeyRequired,
306+
AuthorizationScopes: cgmo.AuthorizationScopes,
307+
AuthorizationType: cgmo.AuthorizationType,
308+
AuthorizerId: cgmo.AuthorizerId,
309+
HttpMethod: cgmo.HttpMethod,
310+
MethodIntegration: cgmo.MethodIntegration,
311+
MethodResponses: cgmo.MethodResponses,
312+
OperationName: cgmo.OperationName,
313+
RequestModels: cgmo.RequestModels,
314+
RequestParameters: cgmo.RequestParameters,
315+
RequestValidatorId: cgmo.RequestValidatorId,
316+
// ResultMetadata: This will be missing or zero value; handle accordingly
317+
}
318+
}
319+
243320
// create a CachedApiGatewayGetUsagePlans function that accepts a client, account id, region. Make sure it handles caching, the region option and pagination if needed
244321
func CachedApiGatewayGetUsagePlans(client APIGatewayClientInterface, accountID string, region string) ([]apiGatewayTypes.UsagePlan, error) {
245322
var PaginationControl *string

aws/sdk/apigatewayv2.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ type APIGatewayv2ClientInterface interface {
2121
}
2222

2323
type domainName apiGatwayV2Types.DomainName
24+
type stage apiGatwayV2Types.Stage
2425

2526
func init() {
2627
gob.Register([]apiGatwayV2Types.Api{})
2728
//need to do this to avoid conflicts with the Instance type in the ec2 package
2829
gob.Register([]domainName{})
2930
gob.Register([]apiGatwayV2Types.ApiMapping{})
30-
gob.Register([]apiGatwayV2Types.Stage{})
31+
gob.Register([]stage{})
3132
gob.Register([]apiGatwayV2Types.Route{})
3233
}
3334

0 commit comments

Comments
 (0)