Skip to content

Commit

Permalink
Merge pull request #2144 from josephschorr/expiring-rels-explicit-option
Browse files Browse the repository at this point in the history
Add explicit option to enable expiration in schema
  • Loading branch information
josephschorr authored Nov 26, 2024
2 parents 3001a3d + 6e73023 commit 3542a0f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
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

0 comments on commit 3542a0f

Please sign in to comment.