Skip to content

Commit

Permalink
feat: add global setting for generator
Browse files Browse the repository at this point in the history
  • Loading branch information
khatibomar committed May 4, 2024
1 parent 7b70e96 commit 09477ab
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
7 changes: 6 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,10 @@ type Mapping struct {
}

type Config struct {
Mappings []Mapping `toml:"mappings"`
Mappings []Mapping `toml:"mappings"`
Settings GenSetting `toml:"settings"`
}

type GenSetting struct {
Style string `toml:"style"`
}
14 changes: 10 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ func main() {
Send()
}

log.Print(cfg)

groupedMappings := groupMappings(cfg.Mappings)

var batchWork [][]work
Expand Down Expand Up @@ -100,7 +102,11 @@ func main() {
var endResult []File

for _, groupedWork := range batchWork {
result, err := Process(groupedWork)
g := Generator{
style: cfg.Settings.Style,
}

result, err := g.Process(groupedWork)
if err != nil {
log.Fatal().
Err(err).
Expand Down Expand Up @@ -137,9 +143,8 @@ func Usage() {
flag.PrintDefaults()
}

func Process(groupedWork []work) (File, error) {
func (g *Generator) Process(groupedWork []work) (File, error) {
var result File
g := Generator{}

if len(groupedWork) == 0 {
return result, errNoWork
Expand Down Expand Up @@ -171,7 +176,8 @@ func groupMappings(mappings []Mapping) map[string][]Mapping {
}

type Generator struct {
buf bytes.Buffer
buf bytes.Buffer
style string
}

func (g *Generator) Printf(format string, args ...any) {
Expand Down
6 changes: 4 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ func TestProcess(t *testing.T) {
}

t.Run("No work", func(t *testing.T) {
_, err := Process(groupedWork)
g := Generator{}
_, err := g.Process(groupedWork)
assert.Equal(t, errNoWork, err)
})

Expand Down Expand Up @@ -396,7 +397,8 @@ func TestProcess(t *testing.T) {
w = generateWorkFromSrcs(code2, "T", code1, "S")
groupedWork = append(groupedWork, w)

f, err := Process(groupedWork)
g := Generator{}
f, err := g.Process(groupedWork)
assert.NoError(t, err)
assert.Equal(t, string(formattedExpectedOutput), string(f.Buf))
})
Expand Down
3 changes: 3 additions & 0 deletions testdata/km.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
[settings]
style="default"

[[mappings]]
[mappings.settings]
override=true
Expand Down

0 comments on commit 09477ab

Please sign in to comment.