Skip to content

Commit 5e52c2b

Browse files
vojtabiberleclaude
andcommitted
DMD-921 - Extract generateEmptySamplesIndex to reduce nesting complexity
Refactor GenerateSamples to extract the empty samples handling into a separate helper function, reducing nesting complexity and fixing the nestif lint warning. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 2c99385 commit 5e52c2b

1 file changed

Lines changed: 25 additions & 16 deletions

File tree

internal/pkg/llm/twinformat/generator.go

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,22 +1219,7 @@ func (g *Generator) GenerateSamples(ctx context.Context, data *ProcessedData, sa
12191219

12201220
// If no samples, still create an empty index for consistency when --with-samples is enabled.
12211221
if len(samples) == 0 {
1222-
if !samplesDirExistedBefore {
1223-
if err := g.fs.Mkdir(ctx, samplesDir); err != nil {
1224-
return errors.Errorf("failed to create samples directory: %w", err)
1225-
}
1226-
samplesDirCreated = true
1227-
}
1228-
if err := g.generateSamplesIndex(ctx, data, samples); err != nil {
1229-
if samplesDirCreated {
1230-
if rmErr := g.fs.Remove(ctx, samplesDir); rmErr != nil {
1231-
g.logger.Warnf(ctx, "Failed to remove samples directory after index generation error: %v", rmErr)
1232-
}
1233-
}
1234-
return errors.Errorf("failed to generate samples index: %w", err)
1235-
}
1236-
g.logger.Info(ctx, "No samples to generate, created empty samples index")
1237-
return nil
1222+
return g.generateEmptySamplesIndex(ctx, data, samplesDir, samplesDirExistedBefore)
12381223
}
12391224

12401225
// Generate individual sample files first, collecting only successful ones.
@@ -1316,6 +1301,30 @@ func (g *Generator) generateSamplesIndex(ctx context.Context, data *ProcessedDat
13161301
return g.jsonWriter.Write(ctx, indexPath, index)
13171302
}
13181303

1304+
// generateEmptySamplesIndex creates an empty samples index when no samples are available.
1305+
// This ensures consistent output structure when --with-samples is enabled.
1306+
func (g *Generator) generateEmptySamplesIndex(ctx context.Context, data *ProcessedData, samplesDir string, dirExisted bool) error {
1307+
dirCreated := false
1308+
if !dirExisted {
1309+
if err := g.fs.Mkdir(ctx, samplesDir); err != nil {
1310+
return errors.Errorf("failed to create samples directory: %w", err)
1311+
}
1312+
dirCreated = true
1313+
}
1314+
1315+
if err := g.generateSamplesIndex(ctx, data, nil); err != nil {
1316+
if dirCreated {
1317+
if rmErr := g.fs.Remove(ctx, samplesDir); rmErr != nil {
1318+
g.logger.Warnf(ctx, "Failed to remove samples directory after index generation error: %v", rmErr)
1319+
}
1320+
}
1321+
return errors.Errorf("failed to generate samples index: %w", err)
1322+
}
1323+
1324+
g.logger.Info(ctx, "No samples to generate, created empty samples index")
1325+
return nil
1326+
}
1327+
13191328
// generateSampleFile generates a sample CSV file and metadata for a table.
13201329
// For new directories, cleans up on failure to avoid partial artifacts.
13211330
// For existing directories, removes old files before writing to prevent mixed state.

0 commit comments

Comments
 (0)