Skip to content

Commit

Permalink
WIP add yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
Theo-Hafsaoui committed Nov 10, 2024
1 parent 78f7aaf commit d646017
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 2 deletions.
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ module anemon

go 1.22.0

require github.com/spf13/cobra v1.8.1
require (
github.com/spf13/cobra v1.8.1
gopkg.in/yaml.v3 v3.0.1
)

require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
56 changes: 56 additions & 0 deletions internal/adapters/input/input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@ import (
)

var(
yamlContent = `
info:
name: Doe
firstname: John
number: "12345"
mail: john.doe@example.com
github: johndoe
linkedin: john-doe-linkedin
variante:
optionA:
- "value1"
- "value2"
optionB:
- "value3"
`
work_input = `
# Back-End Intern
## February 2024 -- August 2024
Expand Down Expand Up @@ -152,3 +167,44 @@ func TestSections(t *testing.T) {

})
}


func TestGetParamsFrom(t *testing.T) {
tempDir, err := os.MkdirTemp("", "test")
if err != nil { t.Fatalf("Failed to create temp directory: %v", err) }
defer os.RemoveAll(tempDir)

yamlFilePath := filepath.Join(tempDir, "params.yml")
err = os.WriteFile(yamlFilePath, []byte(yamlContent), 0644)
if err != nil { t.Fatalf("Failed to write YAML file: %v", err) }

source := &YamlSource{}
params, err := source.GetParamsFrom(tempDir)
if err != nil {
t.Fatalf("GetParamsFrom returned an error: %v", err)
}
expectedParams := Params{
Info: struct {
Name string `yaml:"name"`
FirstName string `yaml:"firstname"`
Number string `yaml:"number"`
Mail string `yaml:"mail"`
GitHub string `yaml:"github"`
LinkedIn string `yaml:"linkedin"`
}{
Name: "Doe",
FirstName: "John",
Number: "12345",
Mail: "john.doe@example.com",
GitHub: "johndoe",
LinkedIn: "john-doe-linkedin",
},
Variante: map[string][]string{
"optionA": {"value1", "value2"},
"optionB": {"value3"},
},
}
if !reflect.DeepEqual(params, expectedParams) {
t.Errorf("Expected %+v, but got %+v", expectedParams, params)
}
}
33 changes: 33 additions & 0 deletions internal/adapters/input/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package input

import (
"os"
"gopkg.in/yaml.v3"
)

type Params struct {
Info struct {
Name string `yaml:"name"`
FirstName string `yaml:"firstname"`
Number string `yaml:"number"`
Mail string `yaml:"mail"`
GitHub string `yaml:"github"`
LinkedIn string `yaml:"linkedin"`
} `yaml:"info"`
Variante map[string][]string `yaml:"variante"`
}

type YamlSource struct{}

func (*YamlSource) GetParamsFrom(root string) (Params, error) {
params := Params{}
yamlFile, err := os.ReadFile(root + "/params.yml")
if err != nil {
return params, err
}
err = yaml.Unmarshal(yamlFile, &params)
if err != nil {
return params, err
}
return params, nil
}
2 changes: 1 addition & 1 deletion internal/core/ports.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type Section struct {
Title string
Paragraphes []Paragraphe
}

type Paragraphe struct {
H1 string
H2 string
Expand Down

0 comments on commit d646017

Please sign in to comment.