Denormalize fallback logic into jobs and pipelines functions

This commit is contained in:
Pieter Noordhuis 2025-01-13 09:51:37 +01:00
parent e4770c66f0
commit 12943dbcff
No known key found for this signature in database
GPG Key ID: 12ACCCC104CF2930
5 changed files with 38 additions and 22 deletions

View File

@ -236,24 +236,6 @@ func (t *translateContext) rewriteValue(ctx context.Context, p dyn.Path, v dyn.V
return dyn.NewValue(out, v.Locations()), nil
}
func (t *translateContext) rewriteRelativeTo(ctx context.Context, p dyn.Path, v dyn.Value, fn rewriteFunc, dir, fallback string) (dyn.Value, error) {
nv, err := t.rewriteValue(ctx, p, v, fn, dir)
if err == nil {
return nv, nil
}
// If we failed to rewrite the path, try to rewrite it relative to the fallback directory.
if fallback != "" {
nv, nerr := t.rewriteValue(ctx, p, v, fn, fallback)
if nerr == nil {
// TODO: Emit a warning that this path should be rewritten.
return nv, nil
}
}
return dyn.InvalidValue, err
}
func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagnostics {
t := &translateContext{
b: b,
@ -279,6 +261,8 @@ func (m *translatePaths) Apply(ctx context.Context, b *bundle.Bundle) diag.Diagn
return diag.FromErr(err)
}
// gatherFallbackPaths collects the fallback paths for relative paths in the configuration.
// Read more about the motivation for this functionality in the "fallback" path translation tests.
func gatherFallbackPaths(v dyn.Value, typ string) (map[string]string, error) {
fallback := make(map[string]string)
pattern := dyn.NewPattern(dyn.Key("resources"), dyn.Key(typ), dyn.AnyKey())

View File

@ -39,7 +39,7 @@ func (t *translateContext) applyArtifactTranslations(ctx context.Context, v dyn.
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for artifact %s: %w", key, err)
}
return t.rewriteRelativeTo(ctx, p, v, rewritePattern.fn, dir, "")
return t.rewriteValue(ctx, p, v, rewritePattern.fn, dir)
})
if err != nil {
return dyn.InvalidValue, err

View File

@ -24,6 +24,6 @@ func (t *translateContext) applyDashboardTranslations(ctx context.Context, v dyn
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for dashboard %s: %w", key, err)
}
return t.rewriteRelativeTo(ctx, p, v, t.retainLocalAbsoluteFilePath, dir, "")
return t.rewriteValue(ctx, p, v, t.retainLocalAbsoluteFilePath, dir)
})
}

View File

@ -44,7 +44,23 @@ func (t *translateContext) applyJobTranslations(ctx context.Context, v dyn.Value
return dyn.InvalidValue, err
}
return t.rewriteRelativeTo(ctx, p, v, rewritePatternFn, dir, fallback[key])
// Try to rewrite the path relative to the directory of the configuration file where the value was defined.
nv, err := t.rewriteValue(ctx, p, v, rewritePatternFn, dir)
if err == nil {
return nv, nil
}
// If we failed to rewrite the path, try to rewrite it relative to the fallback directory.
// We only do this for jobs and pipelines because of the comment in [gatherFallbackPaths].
if fallback[key] != "" {
nv, nerr := t.rewriteValue(ctx, p, v, rewritePatternFn, fallback[key])
if nerr == nil {
// TODO: Emit a warning that this path should be rewritten.
return nv, nil
}
}
return dyn.InvalidValue, err
})
}

View File

@ -51,7 +51,23 @@ func (t *translateContext) applyPipelineTranslations(ctx context.Context, v dyn.
return dyn.InvalidValue, fmt.Errorf("unable to determine directory for pipeline %s: %w", key, err)
}
return t.rewriteRelativeTo(ctx, p, v, rewritePattern.fn, dir, fallback[key])
// Try to rewrite the path relative to the directory of the configuration file where the value was defined.
nv, err := t.rewriteValue(ctx, p, v, rewritePattern.fn, dir)
if err == nil {
return nv, nil
}
// If we failed to rewrite the path, try to rewrite it relative to the fallback directory.
// We only do this for jobs and pipelines because of the comment in [gatherFallbackPaths].
if fallback[key] != "" {
nv, nerr := t.rewriteValue(ctx, p, v, rewritePattern.fn, fallback[key])
if nerr == nil {
// TODO: Emit a warning that this path should be rewritten.
return nv, nil
}
}
return dyn.InvalidValue, err
})
if err != nil {
return dyn.InvalidValue, err