Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add explicit option to enable expiration in schema #2144

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/schemadsl/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (cs CompiledSchema) SourcePositionToRunePosition(source input.Source, posit
type config struct {
skipValidation bool
objectTypePrefix *string
allowedFlags []string
}

func SkipValidation() Option { return func(cfg *config) { cfg.skipValidation = true } }
Expand All @@ -68,6 +69,10 @@ func AllowUnprefixedObjectType() ObjectPrefixOption {
return func(cfg *config) { cfg.objectTypePrefix = new(string) }
}

func AllowExpirationUseFlag() Option {
return func(cfg *config) { cfg.allowedFlags = append(cfg.allowedFlags, "expiration") }
}

type Option func(*config)

type ObjectPrefixOption func(*config)
Expand All @@ -94,6 +99,7 @@ func Compile(schema InputSchema, prefix ObjectPrefixOption, opts ...Option) (*Co
mapper: mapper,
schemaString: schema.SchemaString,
skipValidate: cfg.skipValidation,
allowedFlags: cfg.allowedFlags,
}, root)
if err != nil {
var errorWithNode errorWithNode
Expand Down
2 changes: 1 addition & 1 deletion pkg/schemadsl/compiler/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1032,7 +1032,7 @@ func TestCompile(t *testing.T) {
require := require.New(t)
compiled, err := Compile(InputSchema{
input.Source(test.name), test.input,
}, test.objectPrefix)
}, test.objectPrefix, AllowExpirationUseFlag())

if test.expectedError != "" {
require.Error(err)
Expand Down
6 changes: 6 additions & 0 deletions pkg/schemadsl/compiler/translator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package compiler
import (
"bufio"
"fmt"
"slices"
"strings"

"github.com/ccoveille/go-safecast"
Expand All @@ -22,6 +23,7 @@ type translationContext struct {
mapper input.PositionMapper
schemaString string
skipValidate bool
allowedFlags []string
}

func (tctx translationContext) prefixedPath(definitionName string) (string, error) {
Expand Down Expand Up @@ -650,6 +652,10 @@ func translateSpecificTypeReference(tctx translationContext, typeRefNode *dslNod
return nil, typeRefNode.Errorf("invalid trait: %s", traitName)
}

if !slices.Contains(tctx.allowedFlags, "expiration") {
return nil, typeRefNode.Errorf("expiration trait is not allowed")
}

ref.RequiredExpiration = &core.ExpirationTrait{}
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/schemadsl/generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ definition document {
compiled, err := compiler.Compile(compiler.InputSchema{
Source: input.Source(test.name),
SchemaString: test.input,
}, compiler.AllowUnprefixedObjectType())
}, compiler.AllowUnprefixedObjectType(), compiler.AllowExpirationUseFlag())
require.NoError(err)

source, _, err := GenerateSchema(compiled.OrderedDefinitions)
Expand Down
Loading