Skip to content
Closed
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
33 changes: 32 additions & 1 deletion src/server/templates/swift.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,45 @@ function generateProtocolConformances(protocols: string[]): string {
return protocols.length === 0 ? '' : `: ${protocols.join(', ')}`
}

function swiftStringLiteral(value: string): string {
let escaped = ''
for (const character of value) {
const codePoint = character.codePointAt(0)!
switch (character) {
case '\\':
escaped += '\\\\'
break
case '"':
escaped += '\\"'
break
case '\t':
escaped += '\\t'
break
case '\n':
escaped += '\\n'
break
case '\r':
escaped += '\\r'
break
default:
escaped +=
codePoint < 0x20 || codePoint === 0x7f || codePoint === 0x2028 || codePoint === 0x2029
? `\\u{${codePoint.toString(16)}}`
: character
}
}
return `"${escaped}"`
}

function generateEnum(
enum_: SwiftEnum,
{ accessControl, level }: SwiftGeneratorOptions & { level: number }
): string[] {
return [
`${ident(level)}${accessControl} enum ${enum_.formattedEnumName}${generateProtocolConformances(enum_.protocolConformances)} {`,
...enum_.cases.map(
(case_) => `${ident(level + 1)}case ${case_.formattedName} = "${case_.rawValue}"`
(case_) =>
`${ident(level + 1)}case ${case_.formattedName} = ${swiftStringLiteral(case_.rawValue)}`
),
`${ident(level)}}`,
]
Expand Down
81 changes: 81 additions & 0 deletions test/server/templates/swift.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { describe, expect, test } from 'vitest'

import { apply } from '../../../src/server/templates/swift'
import type { GeneratorMetadata } from '../../../src/lib/generators'
import type {
PostgresColumn,
PostgresSchema,
PostgresTable,
PostgresType,
} from '../../../src/lib/types'

const baseSchema: PostgresSchema = {
id: 1,
name: 'public',
owner: 'postgres',
}

const baseTable = {
id: 1,
schema: 'public',
name: 'tickets',
} as unknown as Omit<PostgresTable, 'columns'>

const baseColumn = (name: string): PostgresColumn =>
({
table_id: 1,
name,
format: 'text',
is_identity: false,
is_generated: false,
is_nullable: false,
default_value: null,
}) as PostgresColumn

const enumType = (enums: string[]): PostgresType =>
({
id: 100,
name: 'status',
schema: 'public',
format: 'status',
enums,
attributes: [],
}) as PostgresType

const buildMetadata = (overrides: Partial<GeneratorMetadata> = {}) => ({
schemas: [baseSchema],
tables: [baseTable],
foreignTables: [],
views: [],
materializedViews: [],
columns: [],
relationships: [],
functions: [],
types: [],
accessControl: 'internal' as const,
...overrides,
})

describe('swift typegen string literal escaping', () => {
test('escapes enum raw values', async () => {
const result = await apply(buildMetadata({ types: [enumType(['say"hi', 'use\\path'])] }))

expect(result).toContain('case sayHi = "say\\"hi"')
expect(result).toContain('case usePath = "use\\\\path"')
})

test('escapes coding key raw values', async () => {
const result = await apply(buildMetadata({ columns: [baseColumn('say"hi')] }))

expect(result).toContain('case sayHi = "say\\"hi"')
})

test('escapes newlines and string interpolation markers', async () => {
const result = await apply(
buildMetadata({ types: [enumType(['line\nbreak', 'value\\(call)'])] })
)

expect(result).toContain('case lineBreak = "line\\nbreak"')
expect(result).toContain('case valueCall = "value\\\\(call)"')
})
})