Skip to content

Commit 7f8c0c6

Browse files
committed
fix: pkg api functionalities
1 parent cfcc4fc commit 7f8c0c6

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

pkg/api/adapters/template_processor/template_processor_adapter.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,16 @@ func (tpa *TemplateProcessorAdapter) processTemplate(content string, variables m
161161

162162
// resolveVariableValue resolves a variable value from the variables map
163163
func (tpa *TemplateProcessorAdapter) resolveVariableValue(variableName string, variables map[string]interface{}) string {
164-
// Handle dot notation (e.g., "foo.bar")
164+
// Handle dot notation (e.g., "foo.bar" or ".foo")
165165
parts := strings.Split(variableName, ".")
166166

167167
var current interface{} = variables
168168
for _, part := range parts {
169+
// Skip empty parts (e.g., from ".foo" -> ["", "foo"])
170+
if part == "" {
171+
continue
172+
}
173+
169174
// Handle array access (e.g., "foo[0]")
170175
if strings.Contains(part, "[") && strings.Contains(part, "]") {
171176
// Extract array name and index
@@ -201,6 +206,15 @@ func (tpa *TemplateProcessorAdapter) resolveVariableValue(variableName string, v
201206
return fmt.Sprintf("%v", current)
202207
}
203208

209+
// getMapKeys returns the keys of a map for debugging
210+
func getMapKeys(m map[string]interface{}) []string {
211+
keys := make([]string, 0, len(m))
212+
for k := range m {
213+
keys = append(keys, k)
214+
}
215+
return keys
216+
}
217+
204218
// validateLaTeXContent performs basic LaTeX validation
205219
func (tpa *TemplateProcessorAdapter) validateLaTeXContent(content string) error {
206220
// Check for basic LaTeX structure

pkg/api/adapters/variable_resolver/variable_resolver_adapter.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ func (vra *VariableResolverAdapter) resolveValue(value interface{}) (string, err
108108
return "", nil
109109
}
110110

111+
// Check if value has ToAutoPDFFormat() method (OCP compliance)
112+
if formatter, ok := value.(interface{ ToAutoPDFFormat() string }); ok {
113+
return formatter.ToAutoPDFFormat(), nil
114+
}
115+
111116
switch v := value.(type) {
112117
case string:
113118
return v, nil

pkg/api/application/pdf_generation_service.go

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package application
55

66
import (
77
"context"
8+
"fmt"
9+
"os"
810
"time"
911

1012
"github.com/BuddhiLW/AutoPDF/pkg/api"
@@ -65,7 +67,11 @@ func (s *PDFGenerationApplicationService) GeneratePDF(ctx context.Context, req g
6567
for k, v := range simpleVariables {
6668
interfaceVars[k] = v
6769
}
68-
_, err = s.templateService.Process(ctx, req.TemplatePath, interfaceVars)
70+
71+
// Debug: Log variables being processed
72+
fmt.Printf("DEBUG: Processing template with variables: %+v\n", interfaceVars)
73+
74+
processedContent, err := s.templateService.Process(ctx, req.TemplatePath, interfaceVars)
6975
if err != nil {
7076
return generation.PDFGenerationResult{
7177
Success: false,
@@ -79,10 +85,52 @@ func (s *PDFGenerationApplicationService) GeneratePDF(ctx context.Context, req g
7985
}, err
8086
}
8187

82-
// Step 4: Generate PDF using external service
88+
// Write processed template to a temporary file
89+
// This ensures the LaTeX engine uses the processed content with variables replaced
90+
tempFile, err := os.CreateTemp("", "autopdf-processed-*.tex")
91+
if err != nil {
92+
return generation.PDFGenerationResult{
93+
Success: false,
94+
Error: domain.TemplateProcessingError{
95+
Code: domain.ErrCodeTemplateInvalid,
96+
Message: "Failed to create temporary file for processed template",
97+
Details: api.NewErrorDetails(api.ErrorCategoryTemplate, api.ErrorSeverityHigh).
98+
WithTemplatePath(req.TemplatePath).
99+
WithError(err),
100+
},
101+
}, err
102+
}
103+
defer os.Remove(tempFile.Name()) // Clean up temporary file after generation
104+
105+
// Debug: Log processed content
106+
contentLen := len(processedContent)
107+
if contentLen > 500 {
108+
contentLen = 500
109+
}
110+
fmt.Printf("DEBUG: Processed template content (first %d chars): %s\n", contentLen, processedContent[:contentLen])
111+
112+
if _, err := tempFile.WriteString(processedContent); err != nil {
113+
tempFile.Close()
114+
return generation.PDFGenerationResult{
115+
Success: false,
116+
Error: domain.TemplateProcessingError{
117+
Code: domain.ErrCodeTemplateInvalid,
118+
Message: "Failed to write processed template to temporary file",
119+
Details: api.NewErrorDetails(api.ErrorCategoryTemplate, api.ErrorSeverityHigh).
120+
WithTemplatePath(req.TemplatePath).
121+
WithError(err),
122+
},
123+
}, err
124+
}
125+
tempFile.Close()
126+
127+
// Debug: Log temporary file path
128+
fmt.Printf("DEBUG: Using temporary template file: %s\n", tempFile.Name())
129+
130+
// Step 4: Generate PDF using external service with processed template
83131
generationReq := generation.PDFGenerationRequest{
84-
TemplatePath: req.TemplatePath,
85-
Variables: req.Variables, // Use original complex variables
132+
TemplatePath: tempFile.Name(), // Use processed template file
133+
Variables: req.Variables, // Keep original variables for metadata
86134
Engine: req.Engine,
87135
OutputPath: req.OutputPath,
88136
Options: req.Options,

0 commit comments

Comments
 (0)