Skip to content

Commit

Permalink
fix: render values passed to generator task (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
twelvelabs authored Mar 7, 2024
1 parent 77e4f2e commit c0f144d
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 24 deletions.
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,6 @@ github.com/Netflix/go-expect v0.0.0-20220104043353-73e0943537d2/go.mod h1:HBCaDe
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY=
github.com/aws/aws-sdk-go v1.44.122/go.mod h1:y4AeaBuwd2Lk+GepC1E9v0qOiTws0MIWAX4oIKwKHZo=
github.com/aws/aws-sdk-go v1.50.33 h1:/SKPJ7ZVPCFOYZyTKo5YdjeUEeOn2J2M0qfDTXWAoEU=
github.com/aws/aws-sdk-go v1.50.33/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/aws/aws-sdk-go v1.50.34 h1:J1LjHzWNN/yVxQDTr0NIlI5vz9xRPvWiNCjQ4+5wh58=
github.com/aws/aws-sdk-go v1.50.34/go.mod h1:LF8svs817+Nz+DmiMQKTO3ubZ/6IaTpq3TjupRn3Eqk=
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d h1:xDfNPAt8lFiC1UJrqV3uuy861HCTo708pDMbjHHdCas=
Expand Down Expand Up @@ -916,8 +914,6 @@ google.golang.org/api v0.96.0/go.mod h1:w7wJQLTM+wvQpNf5JyEcBoxK0RH7EDrh/L4qfsuJ
google.golang.org/api v0.97.0/go.mod h1:w7wJQLTM+wvQpNf5JyEcBoxK0RH7EDrh/L4qfsuJ13s=
google.golang.org/api v0.98.0/go.mod h1:w7wJQLTM+wvQpNf5JyEcBoxK0RH7EDrh/L4qfsuJ13s=
google.golang.org/api v0.100.0/go.mod h1:ZE3Z2+ZOr87Rx7dqFsdRQkRBk36kDtp/h+QpHbB7a70=
google.golang.org/api v0.168.0 h1:MBRe+Ki4mMN93jhDDbpuRLjRddooArz4FeSObvUMmjY=
google.golang.org/api v0.168.0/go.mod h1:gpNOiMA2tZ4mf5R9Iwf4rK/Dcz0fbdIgWYWVoxmsyLg=
google.golang.org/api v0.169.0 h1:QwWPy71FgMWqJN/l6jVlFHUa29a7dcUy02I8o799nPY=
google.golang.org/api v0.169.0/go.mod h1:gpNOiMA2tZ4mf5R9Iwf4rK/Dcz0fbdIgWYWVoxmsyLg=
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
Expand Down
14 changes: 2 additions & 12 deletions internal/cmd/new.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package cmd

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -100,19 +99,10 @@ func (a *NewAction) Run() error {
}
}

// The name may be a direct path to a generator on the filesystem.
generator, err := stamp.NewGeneratorFromPath(a.Store, a.Name)
if err != nil && !errors.Is(err, stamp.ErrNotFound) {
generator, err := a.Store.Load(a.Name)
if err != nil {
return err
}
// If that doesn't return a result, then it must be a named
// generator in the store...
if generator == nil {
generator, err = a.Store.Load(a.Name)
if err != nil {
return err
}
}

// Update usage text w/ info from generator.
a.setUsage(generator)
Expand Down
14 changes: 11 additions & 3 deletions internal/pkg/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

// cspell:disable-line
yaml "gopkg.in/yaml.v3"

"github.com/twelvelabs/stamp/internal/fsutil"
)

var (
Expand Down Expand Up @@ -46,13 +48,19 @@ func PackagePath(root string, name string) (string, error) {

// LoadPackage parses and returns the package at `pkgPath`.
func LoadPackage(pkgPath string, metaFile string) (*Package, error) {
// Ensure package path exists.
if _, err := os.Stat(pkgPath); os.IsNotExist(err) {
// Ensure package path exists and is a directory.
if !fsutil.PathIsDir(pkgPath) {
return nil, NewNotFoundError(metaFile)
}

// Ensure package metadata path exists.
pkgMetaPath := filepath.Join(pkgPath, metaFile)
if fsutil.NoPathExists(pkgMetaPath) {
return nil, NewNotFoundError(metaFile)
}

// Read the package metadata file.
pkgMeta, err := os.ReadFile(filepath.Join(pkgPath, metaFile))
pkgMeta, err := os.ReadFile(pkgMetaPath)
if err != nil {
return nil, fmt.Errorf("load package: %w", err)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestLoadPackage(t *testing.T) {
{
PackageName: "empty",
PackagePath: packageFixtureDir("empty"),
Err: "no such file or directory",
Err: "package not found",
},
{
PackageName: "non-parsable",
Expand Down
25 changes: 22 additions & 3 deletions internal/pkg/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pkg

import (
"context"
"errors"
"fmt"
"os"
"path"
Expand Down Expand Up @@ -41,11 +42,29 @@ func (s *Store) WithMetaFile(filename string) *Store {

// Returns the named package from the store.
func (s *Store) Load(name string) (*Package, error) {
pkgPath, err := s.path(name)
if err != nil {
var pkg *Package
var err error

// The name may be a direct path to a package on the filesystem.
pkg, err = LoadPackage(name, s.MetaFile)
nfErr := NewNotFoundError(s.MetaFile)
if err != nil && !errors.Is(err, nfErr) {
return nil, err
}
return LoadPackage(pkgPath, s.MetaFile)

// If that doesn't return a result, then it must be
// a named package in the store.
// Convert the name to a path and load.
if pkg == nil {
var pkgPath string
pkgPath, err = s.path(name)
if err != nil {
return nil, err
}
pkg, err = LoadPackage(pkgPath, s.MetaFile)
}

return pkg, err
}

// Returns all valid packages in the store.
Expand Down
8 changes: 7 additions & 1 deletion internal/stamp/generator_task.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package stamp
import (
"github.com/mitchellh/copystructure"
"github.com/swaggest/jsonschema-go"
"github.com/twelvelabs/termite/render"

"github.com/twelvelabs/stamp/internal/mdutil"
)
Expand Down Expand Up @@ -67,6 +68,11 @@ func (t *GeneratorTask) Execute(ctx *TaskContext, values map[string]any) error {
return err
}

renderedValues, err := render.Map(t.Values, values)
if err != nil {
return err
}

// Deep copy so that any value mutation done by this generator
// doesn't leak up to the caller.
copied, err := copystructure.Copy(values)
Expand All @@ -77,7 +83,7 @@ func (t *GeneratorTask) Execute(ctx *TaskContext, values map[string]any) error {
// Doing this here (in addition to the setting in GetGenerator) to cover
// the case where the same generator name is used in multiple tasks.
// In that scenario, `values` would contain those from the last task added.
for k, v := range t.Values {
for k, v := range renderedValues {
data[k] = v
}

Expand Down

0 comments on commit c0f144d

Please sign in to comment.