Skip to content

Commit

Permalink
Expose properties from generated structs for custom use (#71)
Browse files Browse the repository at this point in the history
* Remove locale property from generated struct

* Expose properties and helpers of generated struct type
  • Loading branch information
liamnichols authored May 16, 2024
1 parent 6e50766 commit 67c0639
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 205 deletions.
76 changes: 12 additions & 64 deletions Sources/StringGenerator/StringGenerator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,6 @@ public struct StringGenerator {
) {
// BundleDescription
EnumDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate))
],
name: .type(.BundleDescription),
memberBlockBuilder: {
EnumCaseDeclSyntax {
Expand Down Expand Up @@ -104,9 +101,6 @@ public struct StringGenerator {

// Argument
EnumDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate))
],
name: .type(.Argument),
memberBlockBuilder: {
// case object(String)
Expand Down Expand Up @@ -170,47 +164,27 @@ public struct StringGenerator {

// Properties
VariableDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate)),
],
.let,
name: PatternSyntax(IdentifierPatternSyntax(identifier: "key")),
type: TypeAnnotationSyntax(
type: .identifier(.StaticString)
)
)
VariableDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate)),
],
.let,
name: PatternSyntax(IdentifierPatternSyntax(identifier: "arguments")),
type: TypeAnnotationSyntax(
type: ArrayTypeSyntax(element: .identifier(.Argument))
)
)
VariableDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate)),
],
.let,
name: PatternSyntax(IdentifierPatternSyntax(identifier: "table")),
type: TypeAnnotationSyntax(
type: OptionalTypeSyntax(wrappedType: .identifier(.String))
)
)
VariableDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate)),
],
.let,
name: PatternSyntax(IdentifierPatternSyntax(identifier: "locale")),
type: TypeAnnotationSyntax(type: .identifier(.Locale))
)
VariableDeclSyntax(
modifiers: [
DeclModifierSyntax(name: .keyword(.fileprivate)),
],
.let,
name: PatternSyntax(IdentifierPatternSyntax(identifier: "bundle")),
type: TypeAnnotationSyntax(type: .identifier(.BundleDescription))
Expand All @@ -235,10 +209,6 @@ public struct StringGenerator {
firstName: "table",
type: OptionalTypeSyntax(wrappedType: .identifier(.String))
)
FunctionParameterSyntax(
firstName: "locale",
type: .identifier(.Locale)
)
FunctionParameterSyntax(
firstName: "bundle",
type: .identifier(.BundleDescription)
Expand Down Expand Up @@ -271,14 +241,6 @@ public struct StringGenerator {
operator: AssignmentExprSyntax(),
rightOperand: DeclReferenceExprSyntax(baseName: "table")
)
InfixOperatorExprSyntax(
leftOperand: MemberAccessExprSyntax(
base: DeclReferenceExprSyntax(baseName: .keyword(.`self`)),
name: "locale"
),
operator: AssignmentExprSyntax(),
rightOperand: DeclReferenceExprSyntax(baseName: "locale")
)
InfixOperatorExprSyntax(
leftOperand: MemberAccessExprSyntax(
base: DeclReferenceExprSyntax(baseName: .keyword(.`self`)),
Expand Down Expand Up @@ -595,7 +557,6 @@ public struct StringGenerator {

func generateStringsTableArgumentValueExtension() -> ExtensionDeclSyntax {
ExtensionDeclSyntax(
accessLevel: .private,
extendedType: MemberTypeSyntax(
baseType: localTableMemberType,
name: .type(.Argument)
Expand Down Expand Up @@ -707,7 +668,6 @@ public struct StringGenerator {

func generateBundleExtension() -> ExtensionDeclSyntax {
ExtensionDeclSyntax(
accessLevel: .private,
extendedType: .identifier(.Bundle)
) {
FunctionDeclSyntax(
Expand Down Expand Up @@ -1060,38 +1020,30 @@ public struct StringGenerator {
name: .keyword(.`init`)
)
) {
// localizable.key,
LabeledExprSyntax(
label: nil,
expression: MemberAccessExprSyntax(
base: DeclReferenceExprSyntax(baseName: variableToken),
declName: DeclReferenceExprSyntax(baseName: "key")
)
)

// defaultValue: localizable.defaultValue,
LabeledExprSyntax(
label: "defaultValue",
expression: MemberAccessExprSyntax(
base: DeclReferenceExprSyntax(baseName: variableToken),
declName: DeclReferenceExprSyntax(baseName: "defaultValue")
)
)

// table: localizable.table,
LabeledExprSyntax(
label: "table",
expression: MemberAccessExprSyntax(
base: DeclReferenceExprSyntax(baseName: variableToken),
declName: DeclReferenceExprSyntax(baseName: "table")
)
)

LabeledExprSyntax(
label: "locale",
expression: MemberAccessExprSyntax(
base: DeclReferenceExprSyntax(baseName: variableToken),
declName: DeclReferenceExprSyntax(baseName: "locale")
)
)

// bundle: .from(description: localizable.bundle)
LabeledExprSyntax(
label: "bundle",
expression: FunctionCallExprSyntax(
Expand Down Expand Up @@ -1319,22 +1271,18 @@ extension Resource {
baseName: .keyword(.Self)
)
) {
LabeledExprSyntax(label: "key", expression: keyExpr)

LabeledExprSyntax(label: "arguments", expression: argumentsExpr)

LabeledExprSyntax(
label: "table",
expression: StringLiteralExprSyntax(content: table)
label: "key",
expression: keyExpr
)

LabeledExprSyntax(
label: "locale",
expression: MemberAccessExprSyntax(
name: .identifier("current")
)
label: "arguments",
expression: argumentsExpr
)
LabeledExprSyntax(
label: "table",
expression: StringLiteralExprSyntax(content: table)
)

LabeledExprSyntax(
label: "bundle",
expression: MemberAccessExprSyntax(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,34 @@ extension String {
/// value // "Test %"
/// ```
internal struct FormatSpecifiers {
fileprivate enum BundleDescription {
enum BundleDescription {
case main
case atURL(URL)
case forClass(AnyClass)
}

fileprivate enum Argument {
enum Argument {
case object(String)
case int(Int)
case uint(UInt)
case double(Double)
case float(Float)
}

fileprivate let key: StaticString
fileprivate let arguments: [Argument]
fileprivate let table: String?
fileprivate let locale: Locale
fileprivate let bundle: BundleDescription
let key: StaticString
let arguments: [Argument]
let table: String?
let bundle: BundleDescription

fileprivate init(
key: StaticString,
arguments: [Argument],
table: String?,
locale: Locale,
bundle: BundleDescription
) {
self.key = key
self.arguments = arguments
self.table = table
self.locale = locale
self.bundle = bundle
}
}
Expand Down Expand Up @@ -70,7 +67,6 @@ extension String.FormatSpecifiers {
.object(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -89,7 +85,6 @@ extension String.FormatSpecifiers {
.int(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -108,7 +103,6 @@ extension String.FormatSpecifiers {
.int(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -127,7 +121,6 @@ extension String.FormatSpecifiers {
.double(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -146,7 +139,6 @@ extension String.FormatSpecifiers {
.double(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -165,7 +157,6 @@ extension String.FormatSpecifiers {
.int(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -184,7 +175,6 @@ extension String.FormatSpecifiers {
.uint(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -201,7 +191,6 @@ extension String.FormatSpecifiers {
key: "percentage",
arguments: [],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -218,7 +207,6 @@ extension String.FormatSpecifiers {
key: "percentage_escaped",
arguments: [],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -235,7 +223,6 @@ extension String.FormatSpecifiers {
key: "percentage_escaped_space_o",
arguments: [],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -252,7 +239,6 @@ extension String.FormatSpecifiers {
key: "percentage_space_o",
arguments: [],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -271,7 +257,6 @@ extension String.FormatSpecifiers {
.uint(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand All @@ -290,7 +275,6 @@ extension String.FormatSpecifiers {
.uint(arg1)
],
table: "FormatSpecifiers",
locale: .current,
bundle: .current
)
}
Expand Down Expand Up @@ -319,7 +303,7 @@ private extension String.FormatSpecifiers {
}
}

private extension String.FormatSpecifiers.Argument {
extension String.FormatSpecifiers.Argument {
var value: CVarArg {
switch self {
case .int(let value):
Expand Down Expand Up @@ -351,7 +335,7 @@ private extension String.FormatSpecifiers.BundleDescription {
}
}

private extension Bundle {
extension Bundle {
static func from(description: String.FormatSpecifiers.BundleDescription) -> Bundle? {
switch description {
case .main:
Expand Down Expand Up @@ -542,7 +526,6 @@ extension LocalizedStringResource {
formatSpecifiers.key,
defaultValue: formatSpecifiers.defaultValue,
table: formatSpecifiers.table,
locale: formatSpecifiers.locale,
bundle: .from(description: formatSpecifiers.bundle)
)
}
Expand Down
Loading

0 comments on commit 67c0639

Please sign in to comment.