Skip to content

Commit

Permalink
Move code generation to a package
Browse files Browse the repository at this point in the history
  • Loading branch information
shadromani committed Jan 15, 2024
1 parent 0f96025 commit c2dd1a0
Show file tree
Hide file tree
Showing 17 changed files with 48 additions and 42 deletions.
38 changes: 37 additions & 1 deletion generate.go → gen/generate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package main
package gen

Check warning on line 1 in gen/generate.go

View workflow job for this annotation

GitHub Actions / lint

package-comments: should have a package comment (revive)

Check warning on line 1 in gen/generate.go

View workflow job for this annotation

GitHub Actions / lint

package-comments: should have a package comment (revive)

import (
"fmt"
"go/format"
"os"
)

type treeFunction struct {
Expand Down Expand Up @@ -65,3 +66,38 @@ func codegenTree(r *renderer, tree *node, level int) (string, error) {

return r.executeDecisionNode(tree, level, left, right)
}


// GenerateFile generates a .go file containing a function that implements the XGB model.
func GenerateFile(
inputJSON string,
packageName,
funcName,
outputFile string,
) error {
x, err := readModel(inputJSON)
if err != nil {
return err
}

trees, err := readTrees(x)
if err != nil {
return err
}

r, err := newRenderer()
if err != nil {
return err
}

code, err := generateSource(packageName, funcName, trees, r)
if err != nil {
return err
}

if err := os.WriteFile(outputFile, []byte(code), 0o644); err != nil {
return fmt.Errorf("error writing file: %w", err)
}

return nil
}
2 changes: 1 addition & 1 deletion parse.go → gen/parse.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gen

Check failure on line 1 in gen/parse.go

View workflow job for this annotation

GitHub Actions / lint

ST1000: at least one file in a package should have a package comment (stylecheck)

Check failure on line 1 in gen/parse.go

View workflow job for this annotation

GitHub Actions / lint

ST1000: at least one file in a package should have a package comment (stylecheck)

import (
"encoding/json"
Expand Down
2 changes: 1 addition & 1 deletion parse_test.go → gen/parse_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gen

import (
"path/filepath"
Expand Down
2 changes: 1 addition & 1 deletion templates.go → gen/templates.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package main
package gen

Check failure on line 1 in gen/templates.go

View workflow job for this annotation

GitHub Actions / lint

ST1000: at least one file in a package should have a package comment (stylecheck)

Check failure on line 1 in gen/templates.go

View workflow job for this annotation

GitHub Actions / lint

ST1000: at least one file in a package should have a package comment (stylecheck)

import (
"bytes"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 4 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// xgb2code generates code for an XGB model.
// This program generates code for an XGB model.
package main

import (
"flag"
"fmt"
"log"
"os"

"github.com/maxmind/xgb2code/gen"
)

type language string
Expand Down Expand Up @@ -57,42 +59,9 @@ func main() {
os.Exit(1)
}

err := GenerateFile(*inputJSON, *packageName, *funcName, *outputFile)
err := gen.GenerateFile(*inputJSON, *packageName, *funcName, *outputFile)
if err != nil {
log.Fatal(err)
}
}

// GenerateFile generates a .go file containing a function that implements the XGB model.
func GenerateFile(
inputJSON string,
packageName,
funcName,
outputFile string,
) error {
x, err := readModel(inputJSON)
if err != nil {
return err
}

trees, err := readTrees(x)
if err != nil {
return err
}

r, err := newRenderer()
if err != nil {
return err
}

code, err := generateSource(packageName, funcName, trees, r)
if err != nil {
return err
}

if err := os.WriteFile(outputFile, []byte(code), 0o644); err != nil {
return fmt.Errorf("error writing file: %w", err)
}

return nil
}
7 changes: 4 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"path/filepath"
"testing"

"github.com/maxmind/xgb2code/gen"
"github.com/stretchr/testify/require"
)

Expand All @@ -22,7 +23,7 @@ func TestGenerateAndRunModels(t *testing.T) {
t.Run(test.model, func(t *testing.T) {
// Generate the code.

modelDir := filepath.Join("testdata", test.model)
modelDir := filepath.Join("gen","testdata", test.model)

Check failure on line 26 in main_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)

Check failure on line 26 in main_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
modelFile := filepath.Join(modelDir, "model.json")

packageName := "main"
Expand All @@ -31,13 +32,13 @@ func TestGenerateAndRunModels(t *testing.T) {
outputDir := t.TempDir()
funcFile := filepath.Join(outputDir, "predict.go")

err := GenerateFile(modelFile, packageName, functionName, funcFile)
err := gen.GenerateFile(modelFile, packageName, functionName, funcFile)
require.NoError(t, err)

// Copy the test program and test data into place.

files := []string{
filepath.Join("testdata", "main.go"),
filepath.Join("gen","testdata", "main.go"),

Check failure on line 41 in main_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)

Check failure on line 41 in main_test.go

View workflow job for this annotation

GitHub Actions / lint

File is not `gofumpt`-ed with `-extra` (gofumpt)
filepath.Join(modelDir, "xtest.csv"),
filepath.Join(modelDir, "preds.csv"),
}
Expand Down

0 comments on commit c2dd1a0

Please sign in to comment.