Skip to content

Commit

Permalink
fix(spanner): Fixed parsing of Snippets (#1042)
Browse files Browse the repository at this point in the history
* fix(spanner): Fixed parsing of Snippets

* fix(spanner): bug fixes while returning code assessment for files with error.

* lint(spanner): log fix
  • Loading branch information
pratickchokhani authored Mar 5, 2025
1 parent eb861b9 commit 688f5a1
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 11 deletions.
11 changes: 9 additions & 2 deletions assessment/collectors/app_migration_analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,11 @@ func (m *MigrationSummarizer) fetchFileContent(filepath string) (string, error)
}

func (m *MigrationSummarizer) AnalyzeFile(ctx context.Context, filepath string, methodChanges, content string) (*CodeAssessment, []any) {
var codeAssessment *CodeAssessment
emptyAssessment := &CodeAssessment{
Snippets: make([]Snippet, 0),
GeneralWarnings: make([]string, 0),
}
codeAssessment := emptyAssessment

var response string
var isDao bool
Expand Down Expand Up @@ -313,6 +317,7 @@ func (m *MigrationSummarizer) AnalyzeFile(ctx context.Context, filepath string,

response = ParseJSONWithRetries(m.modelFlash, prompt, response, 2, "analyze-dao-class-"+filepath)
isDao = false
logger.Log.Debug("Parsed response:", zap.String("response", response))

methodSignatureChangesResponse, err := m.fetchMethodSignature(response)
if err != nil {
Expand All @@ -326,7 +331,7 @@ func (m *MigrationSummarizer) AnalyzeFile(ctx context.Context, filepath string,
codeAssessment, error := parser.ParseFileAnalyzerResponse(filepath, response, isDao)

if error != nil {
return codeAssessment, methodSignatureChanges
return emptyAssessment, methodSignatureChanges
}

return codeAssessment, methodSignatureChanges
Expand All @@ -351,6 +356,8 @@ func (m *MigrationSummarizer) fetchPublicMethodSignature(fileAnalyzerResponse st

func (m *MigrationSummarizer) fetchMethodSignature(fileAnalyzerResponse string) ([]any, error) {

logger.Log.Debug("Analyze File Response: ", zap.String("", fileAnalyzerResponse))

var responseMapStructure map[string]any
err := json.Unmarshal([]byte(fileAnalyzerResponse), &responseMapStructure)
if err != nil {
Expand Down
26 changes: 17 additions & 9 deletions assessment/collectors/parser/response_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ package assessment

import (
"encoding/json"
"fmt"

. "github.com/GoogleCloudPlatform/spanner-migration-tool/assessment/utils"
"github.com/GoogleCloudPlatform/spanner-migration-tool/logger"
"go.uber.org/zap"
)

func ParseStringArrayInterface(input []any) []string {
Expand All @@ -31,12 +34,17 @@ func ParseStringArrayInterface(input []any) []string {
return parsedStringArray
}

func parseAnyToString(anyType any) string {
return fmt.Sprintf("%v", anyType)
}

func ParseSchemaImpact(schemaImpactResponse map[string]any, filePath string) (*Snippet, error) {
logger.Log.Debug("schemaImpactResponse:", zap.Any("sec: ", schemaImpactResponse))
return &Snippet{
SchemaChange: schemaImpactResponse["schema_change"].(string),
TableName: schemaImpactResponse["table"].(string),
ColumnName: schemaImpactResponse["column"].(string),
NumberOfAffectedLines: schemaImpactResponse["number_of_affected_lines"].(string),
SchemaChange: parseAnyToString(schemaImpactResponse["schema_change"]),
TableName: parseAnyToString(schemaImpactResponse["table"]),
ColumnName: parseAnyToString(schemaImpactResponse["column"]),
NumberOfAffectedLines: parseAnyToString(schemaImpactResponse["number_of_affected_lines"]),
SourceCodeSnippet: ParseStringArrayInterface(schemaImpactResponse["existing_code_lines"].([]any)),
SuggestedCodeSnippet: ParseStringArrayInterface(schemaImpactResponse["new_code_lines"].([]any)),
FileName: filePath,
Expand All @@ -47,13 +55,13 @@ func ParseSchemaImpact(schemaImpactResponse map[string]any, filePath string) (*S
func ParseCodeImpact(codeImpactResponse map[string]any, filePath string) (*Snippet, error) {

return &Snippet{
SourceMethodSignature: codeImpactResponse["original_method_signature"].(string),
SuggestedMethodSignature: codeImpactResponse["new_method_signature"].(string),
SourceMethodSignature: parseAnyToString(codeImpactResponse["original_method_signature"]),
SuggestedMethodSignature: parseAnyToString(codeImpactResponse["new_method_signature"]),
SourceCodeSnippet: ParseStringArrayInterface(codeImpactResponse["code_sample"].([]any)),
SuggestedCodeSnippet: ParseStringArrayInterface(codeImpactResponse["suggested_change"].([]any)),
NumberOfAffectedLines: codeImpactResponse["number_of_affected_lines"].(string),
Complexity: codeImpactResponse["complexity"].(string),
Explanation: codeImpactResponse["description"].(string),
NumberOfAffectedLines: parseAnyToString(codeImpactResponse["number_of_affected_lines"]),
Complexity: parseAnyToString(codeImpactResponse["complexity"]),
Explanation: parseAnyToString(codeImpactResponse["description"]),
FileName: filePath,
IsDao: false,
}, nil
Expand Down

0 comments on commit 688f5a1

Please sign in to comment.