Skip to content

Commit 063ecb4

Browse files
add generator logic and tests
Signed-off-by: Kartikay <kartikay_2101ce32@iitp.ac.in>
1 parent 817d4aa commit 063ecb4

File tree

4 files changed

+61
-4
lines changed

4 files changed

+61
-4
lines changed

pkg/schemadsl/compiler/compiler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func DisallowExpirationFlag() Option {
9191

9292
func DisallowDeprecationFlag() Option {
9393
return func(cfg *config) {
94-
cfg.allowedFlags = lo.Filter(cfg.allowedFlags, func(s string, _ int) bool {
94+
cfg.allowedFlags = slicez.Filter(cfg.allowedFlags, func(s string) bool {
9595
return s != deprecationFlag
9696
})
9797
}

pkg/schemadsl/generator/generator.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,20 @@ func (sg *sourceGenerator) emitNamespace(namespace *core.NamespaceDefinition) er
202202
sg.markNewScope()
203203

204204
for _, relation := range namespace.Relation {
205+
if relation.DeprecationType != core.DeprecationType_DEPRECATED_TYPE_UNSPECIFIED {
206+
sg.flags.Add("deprecation")
207+
sg.append("@deprecated(")
208+
209+
switch relation.DeprecationType {
210+
case core.DeprecationType_DEPRECATED_TYPE_WARNING:
211+
sg.append("warn")
212+
case core.DeprecationType_DEPRECATED_TYPE_ERROR:
213+
sg.append("error")
214+
}
215+
sg.append(")")
216+
sg.appendLine()
217+
}
218+
205219
err := sg.emitRelation(relation)
206220
if err != nil {
207221
return err

pkg/schemadsl/generator/generator_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,38 @@ definition document {
395395
relation viewer: user
396396
}`,
397397
},
398+
{
399+
"deprecation test",
400+
`use deprecation
401+
402+
definition document {
403+
404+
@deprecated(warn)
405+
relation viewer: user
406+
407+
@deprecated(error)
408+
relation editor: user
409+
}`,
410+
`use deprecation
411+
412+
definition document {
413+
@deprecated(warn)
414+
relation viewer: user
415+
@deprecated(error)
416+
relation editor: user
417+
}`,
418+
},
419+
{
420+
"unused deprecation flag",
421+
`use deprecation
422+
423+
definition document{
424+
relation viewer: user
425+
}`,
426+
`definition document {
427+
relation viewer: user
428+
}`,
429+
},
398430
}
399431

400432
for _, test := range tests {

pkg/schemadsl/lexer/flags.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ const (
1313
// FlagTypeChecking indicates that `typechecking` is supported as a first-class
1414
// feature in the schema.
1515
FlagTypeChecking = "typechecking"
16+
17+
// FlagDeprecation indicates that `deprecation` is supported as a first-class
18+
// feature in the schema.
19+
FlagDeprecation = "deprecation"
1620
)
1721

1822
var AllUseFlags []string
@@ -22,9 +26,6 @@ func init() {
2226
slices.Sort(AllUseFlags)
2327
}
2428

25-
// Deprecation Flag
26-
const FlagDeprecation = "deprecation"
27-
2829
type transformer func(lexeme Lexeme) (Lexeme, bool)
2930

3031
// Flags is a map of flag names to their corresponding transformers.
@@ -44,6 +45,16 @@ var Flags = map[string]transformer{
4445

4546
return lexeme, false
4647
},
48+
49+
FlagDeprecation: func(lexeme Lexeme) (Lexeme, bool) {
50+
if lexeme.Kind == TokenTypeIdentifier && lexeme.Value == "deprecation" {
51+
lexeme.Kind = TokenTypeKeyword
52+
return lexeme, true
53+
}
54+
55+
return lexeme, false
56+
},
57+
4758
FlagTypeChecking: func(lexeme Lexeme) (Lexeme, bool) {
4859
// `typechecking` becomes a keyword.
4960
if lexeme.Kind == TokenTypeIdentifier && lexeme.Value == "typechecking" {

0 commit comments

Comments
 (0)