Skip to content

Commit

Permalink
fix: Add EvaluateAlwaysWithRecords function which does not check erro…
Browse files Browse the repository at this point in the history
…r when records are missing

Used on empty manifest file which will be filled using `Save()` Method.
Used on non empty manifest to store new records which will be filled
using `Save()` Method.
  • Loading branch information
Matovidlo committed Dec 19, 2024
1 parent 3f00ab5 commit 49f8406
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion internal/pkg/template/manifest/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (f *file) records() (oms []model.ObjectManifest, err error) {
}
}
if len(out) == 0 {
return nil, errors.New("unable to create template using invalid manifest configuration")
return out, errors.New("unable to create template using invalid manifest configuration")
}

return out, nil
Expand Down
26 changes: 26 additions & 0 deletions internal/pkg/template/manifest/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ func (f *File) Evaluate(ctx context.Context, jsonnetCtx *jsonnet.Context) (*Mani
return m, nil
}

// EvaluateWithoutRecords is used for empty manifests to load context information into `Manifest` structure.
func (f *File) EvaluateAlwaysWithRecords(ctx context.Context, jsonnetCtx *jsonnet.Context) (m *Manifest, err error) {
// Evaluate Jsonnet
content, err := evaluateFile(ctx, f.file, jsonnetCtx)
if err != nil {
return nil, err
}

// Create manifest
m = New()

// Get records, skip error to proceed
records, _ := content.records()

// Set records
if err := m.records.SetRecords(records); err != nil {
return nil, errors.Errorf(`cannot load manifest: %w`, err)
}

// Set main config
m.mainConfig = content.MainConfig

// Return
return m, nil
}

func (f *File) RawContent() string {
return f.file.Content
}
Expand Down
34 changes: 34 additions & 0 deletions internal/pkg/template/manifest/manifest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,44 @@ func cases() []test {
}
}

func TestLoadMinimalManifestFile(t *testing.T) {
t.Parallel()
ctx := context.Background()
for _, c := range cases() {
if c.name != "minimal" {
continue
}

fs := aferofs.NewMemoryFs()

// Write file
path := Path()
require.NoError(t, fs.WriteFile(ctx, filesystem.NewRawFile(path, c.jsonnet)))

// Load
manifestFile, err := Load(ctx, fs)
assert.NotNil(t, manifestFile)
require.NoError(t, err)

// Evaluate
manifest, err := manifestFile.EvaluateAlwaysWithRecords(context.Background(), nil)
assert.NotNil(t, manifest)
require.NoError(t, err)

// Assert
assert.Equal(t, c.records, manifest.records.All(), c.name)
assert.Equal(t, c.mainConfig, manifest.MainConfig())
}
}

func TestLoadManifestFile(t *testing.T) {
t.Parallel()
ctx := context.Background()
for _, c := range cases() {
if c.name == "minimal" {
continue
}

fs := aferofs.NewMemoryFs()

// Write file
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/template/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,8 @@ func (t *Template) evaluate(templateCtx Context) (tmpl *evaluatedTemplate, err e
_, span := t.deps.Telemetry().Tracer().Start(templateCtx, "keboola.go.declarative.template.evaluate")
defer span.End(&err)

// Evaluate manifest
evaluatedManifest, err := t.manifestFile.Evaluate(templateCtx, templateCtx.JsonnetContext())
// Evaluate manifest to always have records
evaluatedManifest, err := t.manifestFile.EvaluateAlwaysWithRecords(templateCtx, templateCtx.JsonnetContext())
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 49f8406

Please sign in to comment.