Templates: improve required and advanced handling (#26371)
This commit is contained in:
parent
43321ac095
commit
478a055e9b
3 changed files with 91 additions and 5 deletions
|
|
@ -366,8 +366,7 @@ func (t *Template) RenderResult(renderMode int, other map[string]any) ([]byte, m
|
|||
}
|
||||
|
||||
// validate required fields from yaml
|
||||
if s == "" && p.IsRequired() && !p.IsDeprecated() && (renderMode == RenderModeUnitTest ||
|
||||
renderMode == RenderModeInstance && !testing.Testing()) {
|
||||
if s == "" && p.IsRequired() && (renderMode == RenderModeUnitTest || renderMode == RenderModeInstance && !testing.Testing()) {
|
||||
// validate required per usage
|
||||
if len(p.Usages) == 0 || slices.Contains(p.Usages, usage) {
|
||||
return nil, nil, fmt.Errorf("missing required `%s`", p.Name)
|
||||
|
|
|
|||
|
|
@ -137,7 +137,7 @@ func (t *TextLanguage) Update(new TextLanguage, always bool) {
|
|||
}
|
||||
|
||||
// MarshalJSON implements the json.Marshaler interface
|
||||
func (t *TextLanguage) MarshalJSON() (out []byte, err error) {
|
||||
func (t TextLanguage) MarshalJSON() ([]byte, error) {
|
||||
mu.Lock()
|
||||
s := t.String(encoderLanguage)
|
||||
mu.Unlock()
|
||||
|
|
@ -232,7 +232,7 @@ func (p *Param) OverwriteProperties(withParam Param) {
|
|||
}
|
||||
|
||||
func (p *Param) IsAdvanced() bool {
|
||||
return p.Advanced
|
||||
return p.Advanced && !p.Required
|
||||
}
|
||||
|
||||
func (p *Param) IsMasked() bool {
|
||||
|
|
@ -244,7 +244,7 @@ func (p *Param) IsPrivate() bool {
|
|||
}
|
||||
|
||||
func (p *Param) IsRequired() bool {
|
||||
return p.Required
|
||||
return p.Required && !p.Deprecated
|
||||
}
|
||||
|
||||
func (p *Param) IsDeprecated() bool {
|
||||
|
|
@ -264,6 +264,16 @@ func (p *Param) yamlQuote(value string) string {
|
|||
return yamlQuote(value)
|
||||
}
|
||||
|
||||
var _ json.Marshaler = (*Param)(nil)
|
||||
|
||||
func (p Param) MarshalJSON() ([]byte, error) {
|
||||
type param Param
|
||||
pp := (param)(p)
|
||||
pp.Required = p.IsRequired()
|
||||
pp.Advanced = p.IsAdvanced()
|
||||
return json.Marshal(pp)
|
||||
}
|
||||
|
||||
// Product contains naming information about a product a template supports
|
||||
type Product struct {
|
||||
Brand string // product brand
|
||||
|
|
|
|||
77
util/templates/types_test.go
Normal file
77
util/templates/types_test.go
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
package templates
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestParamLogic(t *testing.T) {
|
||||
{
|
||||
p := Param{
|
||||
Advanced: true, // omitempty
|
||||
Required: true,
|
||||
}
|
||||
require.False(t, p.IsAdvanced(), "can't be advanced when required")
|
||||
|
||||
{
|
||||
b, err := json.Marshal(p)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"Name":"","Description":"","Help":"","Required":true,"Type":"String"}`, string(b), "Marshal p advanced/required")
|
||||
}
|
||||
{
|
||||
b, err := json.Marshal(&p)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"Name":"","Description":"","Help":"","Required":true,"Type":"String"}`, string(b), "Marshal *p advanced/required")
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
p := Param{
|
||||
Deprecated: true,
|
||||
Required: true, // omitempty
|
||||
}
|
||||
require.False(t, p.IsRequired(), "can't be required when deprecated")
|
||||
|
||||
{
|
||||
b, err := json.Marshal(p)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"Name":"","Description":"","Help":"","Deprecated":true,"Type":"String"}`, string(b), "Marshal p deprecated/required")
|
||||
}
|
||||
{
|
||||
b, err := json.Marshal(&p)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"Name":"","Description":"","Help":"","Deprecated":true,"Type":"String"}`, string(b), "Marshal *p deprecated/required")
|
||||
}
|
||||
}
|
||||
|
||||
b, err := json.Marshal(Param{
|
||||
Deprecated: true,
|
||||
Advanced: true, // omitempty
|
||||
Required: true, // omitempty
|
||||
})
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, `{"Name":"","Description":"","Help":"","Deprecated":true,"Type":"String"}`, string(b))
|
||||
}
|
||||
|
||||
func TestParamMarshal(t *testing.T) {
|
||||
{
|
||||
p := Param{
|
||||
Description: TextLanguage{Generic: "foo"},
|
||||
}
|
||||
|
||||
{
|
||||
b, err := json.Marshal(p)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"Name":"","Description":"foo","Help":"","Type":"String"}`, string(b))
|
||||
}
|
||||
|
||||
{
|
||||
b, err := json.Marshal(&p)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, `{"Name":"","Description":"foo","Help":"","Type":"String"}`, string(b))
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue