Skip to content

Commit

Permalink
Change service to builder patern
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 16, 2024
1 parent 4de166a commit 0820f7d
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 19 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ run-docker:

clean:
rm ./assets/latex/output/*
rm ./assets/latex/output/*

build:
go build
Expand Down
16 changes: 9 additions & 7 deletions internal/adapters/input/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@ import (

//Use the implementation for markdown and latex to generate latex CV from a tree dir of mardown document
func GenerateCVFromMarkDownToLatex(root string)error{
var source core.Source = &MarkdownSource{}
var paramsSource core.SourceParams = &YamlSource{}
var templateReader core.TemplateReader = &output.LatexReader{}
var templateProccesor core.TemplateProcessor = &output.LatexProccesor{}
var compiler core.Compiler = &output.LatexCompiler{}
service := &core.CVService{}
return service.GenerateTemplates(root,source, paramsSource,templateReader,templateProccesor,compiler)
var builder core.BuilderService = core.BuilderService{}
builder.SetRoot(root)
builder.SetSource(&MarkdownSource{})
builder.SetParamsSource(&YamlSource{})
builder.SetTemplateReader(&output.LatexReader{})
builder.SetTemplateProcessor(&output.LatexProccesor{})
builder.SetCompiler(&output.LatexCompiler{})
service := builder.GetService()
return service.GenerateTemplates()
}
67 changes: 59 additions & 8 deletions internal/core/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,77 @@ import (
"strings"
)

type CVService struct{}
type CVService struct{
root string
source Source
paramsSource SourceParams
templateReader TemplateReader
templateProcessor TemplateProcessor
compiler Compiler
}

type BuilderService struct{
root string
source Source
paramsSource SourceParams
templateReader TemplateReader
templateProcessor TemplateProcessor
compiler Compiler
}

func (cv *BuilderService) SetRoot(root string) {
cv.root = root
}

func (cv *BuilderService) SetSource(source Source) {
cv.source = source
}

func (cv *BuilderService) SetParamsSource(paramsSource SourceParams) {
cv.paramsSource = paramsSource
}

func (cv *BuilderService) SetTemplateReader(templateReader TemplateReader) {
cv.templateReader = templateReader
}

func (cv *BuilderService) SetTemplateProcessor(templateProcessor TemplateProcessor) {
cv.templateProcessor = templateProcessor
}

func (cv *BuilderService) SetCompiler(compiler Compiler) {
cv.compiler = compiler
}

func (s *BuilderService) GetService() CVService {
return CVService{
root: s.root,
source: s.source,
paramsSource: s.paramsSource,
templateReader: s.templateReader,
templateProcessor: s.templateProcessor,
compiler: s.compiler,
}
}


//generate the template for the cvs defined in the assets directory
func (g *CVService) GenerateTemplates(root string, source Source, paramsSource SourceParams,
templateReader TemplateReader, templateProcessor TemplateProcessor, compiler Compiler)error{
func (s *CVService) GenerateTemplates()error{
slog.Info("--Generating CVs--")
cvs,err := source.GetCVsFrom(root);if err != nil{ return err }
params,err := paramsSource.GetParamsFrom(root);if err != nil{ return err }
generiqueTemplate,err := templateReader.ReadCVTemplate(root,params); if err != nil{
cvs,err := s.source.GetCVsFrom(s.root);if err != nil{ return err }
params,err := s.paramsSource.GetParamsFrom(s.root);if err != nil{ return err }
generiqueTemplate,err := s.templateReader.ReadCVTemplate(s.root,params); if err != nil{
return err
}

for _, cv := range cvs {
err = generateCVFrom(cv,params,root,generiqueTemplate,templateProcessor)
err = generateCVFrom(cv,params,s.root,generiqueTemplate,s.templateProcessor)
if err != nil{
return err
}
}

err = compiler.CompileTemplate(root); if err != nil{ return err }
err = s.compiler.CompileTemplate(s.root); if err != nil{ return err }
return nil
}

Expand Down
28 changes: 24 additions & 4 deletions internal/core/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,19 @@ func TestGenerateTemplates(t *testing.T) {
templateReader := &MockTemplateReader{ Template: baseTemplate }
templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) }
compiler := &MockCompiler{}
service := CVService{}
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler )
var builder BuilderService

builder.SetRoot(root)
builder.SetSource(source)
builder.SetParamsSource(paramsSource)
builder.SetTemplateReader(templateReader)
builder.SetTemplateProcessor(templateProcessor)
builder.SetCompiler(compiler)

service := builder.GetService()

err := service.GenerateTemplates()


if err != nil { t.Fatalf("expected no error, got %v", err) }
if len(templateProcessor.GeneratedFiles) != 2 { t.Fatalf("expected 2 generated file, got %d", len(templateProcessor.GeneratedFiles)) }
Expand All @@ -131,8 +142,17 @@ func TestGenerateTemplates(t *testing.T) {
templateReader := &MockTemplateReader{ Template: baseTemplate }
templateProcessor := &MockTemplateProcessor{ GeneratedFiles: make(map[string]string) }
compiler := &MockCompiler{}
service := CVService{}
err := service.GenerateTemplates(root, source, paramsSource, templateReader, templateProcessor, compiler)
var builder BuilderService

builder.SetRoot(root)
builder.SetSource(source)
builder.SetParamsSource(paramsSource)
builder.SetTemplateReader(templateReader)
builder.SetTemplateProcessor(templateProcessor)
builder.SetCompiler(compiler)

service := builder.GetService()
err := service.GenerateTemplates()

if err != nil { t.Fatalf("expected no error, got %v", err) }
if len(templateProcessor.GeneratedFiles) != 1 { t.Fatalf("expected 1 generated file, got %d", len(templateProcessor.GeneratedFiles)) }
Expand Down

0 comments on commit 0820f7d

Please sign in to comment.