Skip to content

Commit cfb7440

Browse files
committed
extract nested properties from objects
1 parent e7005f4 commit cfb7440

File tree

1 file changed

+29
-9
lines changed

1 file changed

+29
-9
lines changed

src/analyze/analyzeGoFile.js

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ function buildTypeContext(ast) {
8181
if (node.args) {
8282
for (const arg of node.args) {
8383
if (arg.name && arg.type) {
84-
context.functions[node.name].params[arg.name] = arg.type;
84+
context.functions[node.name].params[arg.name] = { type: arg.type };
8585
}
8686
}
8787
}
@@ -94,7 +94,7 @@ function buildTypeContext(ast) {
9494
// Global variable declarations
9595
if (node.names && node.names.length > 0 && node.type) {
9696
for (const name of node.names) {
97-
context.globals[name] = node.type;
97+
context.globals[name] = { type: node.type, value: node.value };
9898
}
9999
}
100100
}
@@ -109,7 +109,7 @@ function scanForDeclarations(body, locals) {
109109
if (stmt.tag === 'declare') {
110110
if (stmt.names && stmt.type) {
111111
for (const name of stmt.names) {
112-
locals[name] = stmt.type;
112+
locals[name] = { type: stmt.type, value: stmt.value };
113113
}
114114
}
115115
} else if (stmt.tag === 'if' && stmt.body) {
@@ -914,21 +914,36 @@ function getPropertyInfo(value, typeContext, currentFunction) {
914914
}
915915
// Look up the variable type in the context
916916
const varName = value.value;
917+
let varInfo = null;
917918

918919
// Check function parameters first
919920
if (typeContext && currentFunction && typeContext.functions[currentFunction]) {
920921
const funcContext = typeContext.functions[currentFunction];
921922
if (funcContext.params[varName]) {
922-
return mapGoTypeToSchemaType(funcContext.params[varName]);
923-
}
924-
if (funcContext.locals[varName]) {
925-
return mapGoTypeToSchemaType(funcContext.locals[varName]);
923+
varInfo = funcContext.params[varName];
924+
} else if (funcContext.locals[varName]) {
925+
varInfo = funcContext.locals[varName];
926926
}
927927
}
928928

929929
// Check global variables
930-
if (typeContext && typeContext.globals[varName]) {
931-
return mapGoTypeToSchemaType(typeContext.globals[varName]);
930+
if (!varInfo && typeContext && typeContext.globals[varName]) {
931+
varInfo = typeContext.globals[varName];
932+
}
933+
934+
if (varInfo) {
935+
// If we have a value stored for this variable, analyze it to get nested properties
936+
if (varInfo.value && varInfo.type && varInfo.type.tag === 'map') {
937+
// The variable has a map type and a value, extract properties from the value
938+
const nestedProps = {};
939+
extractPropertiesFromExpr(varInfo.value, nestedProps, null, typeContext, currentFunction);
940+
return {
941+
type: 'object',
942+
properties: nestedProps
943+
};
944+
}
945+
// Otherwise just return the type
946+
return mapGoTypeToSchemaType(varInfo.type);
932947
}
933948

934949
// Otherwise it's an unknown variable reference
@@ -1095,6 +1110,11 @@ function getPropertyInfo(value, typeContext, currentFunction) {
10951110
function mapGoTypeToSchemaType(goType) {
10961111
if (!goType) return { type: 'any' };
10971112

1113+
// Handle case where goType might be an object with a type property
1114+
if (goType.type) {
1115+
goType = goType.type;
1116+
}
1117+
10981118
// Handle simple types
10991119
if (goType.tag === 'string') return { type: 'string' };
11001120
if (goType.tag === 'bool') return { type: 'boolean' };

0 commit comments

Comments
 (0)