Skip to content

Commit

Permalink
Merge pull request #17 from Nasfame/feat/template-function-subt
Browse files Browse the repository at this point in the history
feat: template function subt
  • Loading branch information
lukemarsden authored Oct 21, 2023
2 parents 9613318 + 5b4f389 commit bd71678
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
29 changes: 26 additions & 3 deletions pkg/module/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ import (
"strings"
"text/template"

"github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/rs/zerolog/log"

"github.com/bacalhau-project/lilypad/pkg/data"
"github.com/bacalhau-project/lilypad/pkg/module/shortcuts"
"github.com/bacalhau-project/lilypad/pkg/system"
git "github.com/go-git/go-git/v5"
"github.com/go-git/go-git/v5/plumbing"
"github.com/rs/zerolog/log"
)

const REPO_DIR = "repos"
Expand Down Expand Up @@ -172,6 +173,25 @@ func PrepareModule(module data.ModuleConfig) (string, error) {
return string(fileContents), nil
}

func subst(format string, jsonEncodedInputs ...string) string {

jsonDecodedInputs := make([]any, 0, len(jsonEncodedInputs))

for _, input := range jsonEncodedInputs {
var s string

if err := json.Unmarshal([]byte(input), &s); err != nil {
log.Debug().AnErr("subst: json unmarshall", err).Msgf("input:%s", input)
panic("subst: invalid input")
}

jsonDecodedInputs = append(jsonDecodedInputs, s)
}
log.Printf("jsonDecodedInputs:%v", jsonDecodedInputs)

return fmt.Sprintf(format, jsonDecodedInputs...)
}

// - prepare the module - now we have the text of the template
// - inject the given values using template syntax
// - JSON parse and check we don't have errors
Expand All @@ -184,6 +204,9 @@ func LoadModule(module data.ModuleConfig, inputs map[string]string) (*data.Modul

templateName := fmt.Sprintf("%s-%s-%s", module.Repo, module.Path, module.Hash)
tmpl, err := template.New(templateName).Parse(moduleText)
tmpl.Funcs(template.FuncMap{
"subst": subst,
})
if err != nil {
return nil, err
}
Expand Down
28 changes: 27 additions & 1 deletion pkg/module/utils_test.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package module

import (
"encoding/json"
"fmt"
"testing"

"github.com/bacalhau-project/lilypad/pkg/data"
"github.com/stretchr/testify/assert"

"github.com/bacalhau-project/lilypad/pkg/data"
)

func TestPrepareModule(t *testing.T) {
Expand All @@ -29,3 +31,27 @@ func TestLoadModule(t *testing.T) {
assert.Equal(t, "grycap/cowsay@sha256:fad516b39e3a587f33ce3dbbb1e646073ef35e0b696bcf9afb1a3e399ce2ab0b", module.Job.Spec.Docker.Image)
assert.Equal(t, "Hello, world!", module.Job.Spec.Docker.Entrypoint[1])
}

// TestSubst: [subst] can correctly substitute json encoded values into the template string.
func TestSubst(t *testing.T) {
format := "Hello, %s!"
inputs := []string{"hiro"}
expectedOutput := "Hello, hiro!"

jsonEncodedInputs := make([]string, 0, len(inputs))

for _, input := range inputs {
inputJ, err := json.Marshal(input)
if err != nil {
t.Fatalf("json marshall failed %v", err)
}
jsonEncodedInputs = append(jsonEncodedInputs, string(inputJ))
}
t.Logf("jsonEncodedInputs -%v %d", jsonEncodedInputs, len(jsonEncodedInputs))

actualOutput := subst(format, jsonEncodedInputs...)

if actualOutput != expectedOutput {
t.Errorf("Expected output: %s, but got: %s", expectedOutput, actualOutput)
}
}

0 comments on commit bd71678

Please sign in to comment.