@@ -49,32 +49,56 @@ extension TableMacro: ExtensionMacro {
4949 let selfRewriter = SelfRewriter (
5050 selfEquivalent: type. as ( IdentifierTypeSyntax . self) ? . name ?? " QueryValue "
5151 )
52- let tableName : ExprSyntax
53- if case let . argumentList( arguments) = node. arguments,
54- let expression = arguments. first? . expression
55- {
56- if node. attributeName. identifier == " _Draft " {
57- let memberAccess = expression. cast ( MemberAccessExprSyntax . self)
58- let base = memberAccess. base!
59- draftTableType = TypeSyntax ( " \( base) " )
60- tableName = " \( base) .tableName "
61- } else {
62- if !expression. isNonEmptyStringLiteral {
63- diagnostics. append (
64- Diagnostic (
65- node: expression,
66- message: MacroExpansionErrorMessage ( " Argument must be a non-empty string literal " )
67- )
68- )
52+ var schemaName : ExprSyntax ?
53+ var tableName = ExprSyntax (
54+ StringLiteralExprSyntax (
55+ content: declaration. name. trimmed. text. lowerCamelCased ( ) . pluralized ( )
56+ )
57+ )
58+ if case let . argumentList( arguments) = node. arguments {
59+ for argumentIndex in arguments. indices {
60+ let argument = arguments [ argumentIndex]
61+ switch argument. label {
62+ case nil :
63+ if node. attributeName. identifier == " _Draft " {
64+ let memberAccess = argument. expression. cast ( MemberAccessExprSyntax . self)
65+ let base = memberAccess. base!
66+ draftTableType = TypeSyntax ( " \( base) " )
67+ tableName = " \( base) .tableName "
68+ } else {
69+ if !argument. expression. isNonEmptyStringLiteral {
70+ diagnostics. append (
71+ Diagnostic (
72+ node: argument. expression,
73+ message: MacroExpansionErrorMessage ( " Argument must be a non-empty string literal " )
74+ )
75+ )
76+ }
77+ tableName = argument. expression. trimmed
78+ }
79+
80+ case let . some( label) where label. text == " schema " :
81+ if node. attributeName. identifier == " _Draft " {
82+ let memberAccess = argument. expression. cast ( MemberAccessExprSyntax . self)
83+ let base = memberAccess. base!
84+ draftTableType = TypeSyntax ( " \( base) " )
85+ schemaName = " \( base) .schemaName "
86+ } else {
87+ if !argument. expression. isNonEmptyStringLiteral {
88+ diagnostics. append (
89+ Diagnostic (
90+ node: argument. expression,
91+ message: MacroExpansionErrorMessage ( " Argument must be a non-empty string literal " )
92+ )
93+ )
94+ }
95+ schemaName = argument. expression. trimmed
96+ }
97+
98+ case let argument? :
99+ fatalError ( " Unexpected argument: \( argument) " )
69100 }
70- tableName = expression. trimmed
71101 }
72- } else {
73- tableName = ExprSyntax (
74- StringLiteralExprSyntax (
75- content: declaration. name. trimmed. text. lowerCamelCased ( ) . pluralized ( )
76- )
77- )
78102 }
79103 for member in declaration. memberBlock. members {
80104 guard
@@ -545,6 +569,12 @@ extension TableMacro: ExtensionMacro {
545569 }
546570
547571 var typeAliases : [ DeclSyntax ] = [ ]
572+ var letSchemaName : DeclSyntax ?
573+ if let schemaName {
574+ letSchemaName = """
575+ public static let schemaName: Swift.String? = \( schemaName)
576+ """
577+ }
548578 var initDecoder : DeclSyntax ?
549579 if declaration. hasMacroApplication ( " Selection " ) {
550580 conformances. append ( " \( moduleName) .PartialSelectStatement " )
@@ -579,7 +609,7 @@ extension TableMacro: ExtensionMacro {
579609 }
580610 } \( draft) \( typeAliases, separator: " \n " )
581611 public static let columns = TableColumns()
582- public static let tableName = \( tableName) \( initDecoder) \( initFromOther)
612+ public static let tableName = \( tableName) \( letSchemaName ) \( initDecoder) \( initFromOther)
583613 }
584614 """
585615 )
0 commit comments