fix: Enhance handling of complex object responses in ResourceGenerator

This commit is contained in:
Nawaz Dhandala
2026-01-16 20:24:42 +00:00
parent d153fc4cd4
commit 5c84699bae
2 changed files with 37 additions and 7 deletions

View File

@@ -1276,12 +1276,30 @@ func (r *${resourceTypeName}Resource) Delete(ctx context.Context, req resource.D
// ${fieldName} value is already set from the existing state
}`;
} else if (isComplexObject) {
// For complex object strings, convert API object response to JSON string
// For complex object strings, first check if it's a simple value wrapper type
// (like Version, ObjectID, etc. with _type and value fields), extract the value
// Otherwise, convert API object response to JSON string
return `if val, ok := ${responseValue}.(map[string]interface{}); ok {
if jsonBytes, err := json.Marshal(val); err == nil {
${fieldName} = types.StringValue(string(jsonBytes))
// Check if this is a value wrapper type (e.g., Version, ObjectID with _type and value fields)
if _, hasType := val["_type"]; hasType {
if strVal, ok := val["value"].(string); ok && strVal != "" {
${fieldName} = types.StringValue(strVal)
} else {
${fieldName} = types.StringNull()
}
} else if strVal, ok := val["_id"].(string); ok && strVal != "" {
// Handle ObjectID type responses
${fieldName} = types.StringValue(strVal)
} else if strVal, ok := val["value"].(string); ok && strVal != "" {
// Handle other value wrapper types
${fieldName} = types.StringValue(strVal)
} else {
${fieldName} = types.StringNull()
// Fall back to JSON marshalling for truly complex objects
if jsonBytes, err := json.Marshal(val); err == nil {
${fieldName} = types.StringValue(string(jsonBytes))
} else {
${fieldName} = types.StringNull()
}
}
} else if val, ok := ${responseValue}.(string); ok && val != "" {
${fieldName} = types.StringValue(val)
@@ -1290,7 +1308,7 @@ func (r *${resourceTypeName}Resource) Delete(ctx context.Context, req resource.D
}`;
}
return `if obj, ok := ${responseValue}.(map[string]interface{}); ok {
// Handle ObjectID type responses
// Handle ObjectID and value wrapper type responses (e.g., Version with _type and value fields)
if val, ok := obj["_id"].(string); ok && val != "" {
${fieldName} = types.StringValue(val)
} else if val, ok := obj["value"].(string); ok && val != "" {

View File

@@ -287,6 +287,7 @@
"resolved": "https://registry.npmjs.org/@babel/core/-/core-7.20.5.tgz",
"integrity": "sha512-UdOWmk4pNWTm/4DlPUl/Pt4Gz4rcEMb7CY0Y3eJl5Yz1vI8ZJGmHWaVE55LoxRjdpx0z259GE9U5STA9atUinQ==",
"dev": true,
"peer": true,
"dependencies": {
"@ampproject/remapping": "^2.1.0",
"@babel/code-frame": "^7.18.6",
@@ -1274,6 +1275,7 @@
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz",
"integrity": "sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==",
"dev": true,
"peer": true,
"dependencies": {
"jest-matcher-utils": "^27.0.0",
"pretty-format": "^27.0.0"
@@ -1294,7 +1296,8 @@
"node_modules/@types/node": {
"version": "17.0.45",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz",
"integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw=="
"integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==",
"peer": true
},
"node_modules/@types/prettier": {
"version": "2.7.1",
@@ -1388,6 +1391,7 @@
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz",
"integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==",
"license": "MIT",
"peer": true,
"dependencies": {
"fast-deep-equal": "^3.1.3",
"fast-uri": "^3.0.1",
@@ -1627,6 +1631,7 @@
"url": "https://tidelift.com/funding/github/npm/browserslist"
}
],
"peer": true,
"dependencies": {
"caniuse-lite": "^1.0.30001400",
"electron-to-chromium": "^1.4.251",
@@ -2808,6 +2813,7 @@
"resolved": "https://registry.npmjs.org/jest/-/jest-27.5.1.tgz",
"integrity": "sha512-Yn0mADZB89zTtjkPJEXwrac3LHudkQMR+Paqa8uxJHCBr9agxztUifWCyiYrjhMPBoUVBjyny0I7XH6ozDr7QQ==",
"dev": true,
"peer": true,
"dependencies": {
"@jest/core": "^27.5.1",
"import-local": "^3.0.2",
@@ -4874,6 +4880,7 @@
"resolved": "https://registry.npmjs.org/@babel/core/-/core-7.20.5.tgz",
"integrity": "sha512-UdOWmk4pNWTm/4DlPUl/Pt4Gz4rcEMb7CY0Y3eJl5Yz1vI8ZJGmHWaVE55LoxRjdpx0z259GE9U5STA9atUinQ==",
"dev": true,
"peer": true,
"requires": {
"@ampproject/remapping": "^2.1.0",
"@babel/code-frame": "^7.18.6",
@@ -5641,6 +5648,7 @@
"resolved": "https://registry.npmjs.org/@types/jest/-/jest-27.5.2.tgz",
"integrity": "sha512-mpT8LJJ4CMeeahobofYWIjFo0xonRS/HfxnVEPMPFSQdGUt1uHCnoPT7Zhb+sjDU2wz0oKV0OLUR0WzrHNgfeA==",
"dev": true,
"peer": true,
"requires": {
"jest-matcher-utils": "^27.0.0",
"pretty-format": "^27.0.0"
@@ -5659,7 +5667,8 @@
"@types/node": {
"version": "17.0.45",
"resolved": "https://registry.npmjs.org/@types/node/-/node-17.0.45.tgz",
"integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw=="
"integrity": "sha512-w+tIMs3rq2afQdsPJlODhoUEKzFP1ayaoyl1CcnwtIlsVe7K7bA1NGm4s3PraqTLlXnbIN84zuBlxBWo1u9BLw==",
"peer": true
},
"@types/prettier": {
"version": "2.7.1",
@@ -5736,6 +5745,7 @@
"version": "8.17.1",
"resolved": "https://registry.npmjs.org/ajv/-/ajv-8.17.1.tgz",
"integrity": "sha512-B/gBuNg5SiMTrPkC+A2+cW0RszwxYmn6VYxB/inlBStS5nx6xHIt/ehKRhIMhqusl7a8LjQoZnjCs5vhwxOQ1g==",
"peer": true,
"requires": {
"fast-deep-equal": "^3.1.3",
"fast-uri": "^3.0.1",
@@ -5908,6 +5918,7 @@
"resolved": "https://registry.npmjs.org/browserslist/-/browserslist-4.21.4.tgz",
"integrity": "sha512-CBHJJdDmgjl3daYjN5Cp5kbTf1mUhZoS+beLklHIvkOWscs83YAhLlF3Wsh/lciQYAcbBJgTOD44VtG31ZM4Hw==",
"dev": true,
"peer": true,
"requires": {
"caniuse-lite": "^1.0.30001400",
"electron-to-chromium": "^1.4.251",
@@ -6894,6 +6905,7 @@
"resolved": "https://registry.npmjs.org/jest/-/jest-27.5.1.tgz",
"integrity": "sha512-Yn0mADZB89zTtjkPJEXwrac3LHudkQMR+Paqa8uxJHCBr9agxztUifWCyiYrjhMPBoUVBjyny0I7XH6ozDr7QQ==",
"dev": true,
"peer": true,
"requires": {
"@jest/core": "^27.5.1",
"import-local": "^3.0.2",