Skip to content

Commit 23c97c1

Browse files
authored
Add Swift Format (#2)
Co-authored-by: ajevans99 <ajevans99@users.noreply.github.com>
1 parent ade72e0 commit 23c97c1

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+886
-1072
lines changed

Diff for: .github/pull_request_template.md

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
## Description
2+
3+
Please include a summary of the changes and the related issue. Please also include relevant motivation and context.
4+
5+
## Type of Change
6+
7+
- [ ] Bug fix
8+
- [ ] New feature
9+
- [ ] Breaking change
10+
- [ ] Documentation update
11+
12+
## Additional Notes
13+
14+
Add any other context or screenshots about the pull request here.
15+
16+
---
17+
18+
**Note:** You can add the `auto-format` label to this pull request to enable automatic Swift formatting.

Diff for: .github/workflows/swift.yml renamed to .github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# This workflow will build a Swift project
22
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift
33

4-
name: Swift
4+
name: CI
55

66
on:
77
push:

Diff for: .github/workflows/format.yml

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Format
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
pull_request:
7+
branches: [ "main" ]
8+
9+
jobs:
10+
format:
11+
runs-on: macos-latest
12+
13+
permissions:
14+
contents: write
15+
16+
name: Lint and format
17+
steps:
18+
- uses: actions/checkout@v4
19+
with:
20+
token: ${{ secrets.PAT }}
21+
ref: ${{ github.head_ref }}
22+
23+
- name: Install swift-format
24+
run: brew install swift-format
25+
26+
- name: Format
27+
run: swift-format format --in-place --parallel --recursive Sources/ Tests/
28+
29+
- name: Check for formatting changes
30+
id: check_changes
31+
run: |
32+
git diff --exit-code
33+
continue-on-error: true
34+
35+
- name: Check if PR has a specific label
36+
id: check_label
37+
uses: actions/github-script@v6
38+
with:
39+
result-encoding: string
40+
script: |
41+
const labelToCheck = 'auto-format';
42+
const prLabels = context.payload.pull_request.labels.map(label => label.name);
43+
return prLabels.includes(labelToCheck);
44+
45+
- name: Apply auto-formatting
46+
if: steps.check_changes.outcome == 'failure' && steps.check_label.outputs.result == 'true'
47+
uses: stefanzweifel/git-auto-commit-action@v5
48+
with:
49+
commit_message: Apply auto-formatting
50+
51+
- name: Fail if changes detected (optional)
52+
if: steps.check_changes.outcome == 'failure' && steps.check_label.outputs.result == 'false'
53+
run: |
54+
echo "::error::Formatting issues detected. Please fix them."
55+
exit 1
56+
57+
- name: Lint
58+
id: lint
59+
run: |
60+
swift-format lint --strict --parallel --recursive Sources/ Tests/

Diff for: .swift-format

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"fileScopedDeclarationPrivacy" : {
3+
"accessLevel" : "private"
4+
},
5+
"indentConditionalCompilationBlocks" : true,
6+
"indentSwitchCaseLabels" : false,
7+
"indentation" : {
8+
"spaces" : 2
9+
},
10+
"lineBreakAroundMultilineExpressionChainComponents" : true,
11+
"lineBreakBeforeControlFlowKeywords" : false,
12+
"lineBreakBeforeEachArgument" : true,
13+
"lineBreakBeforeEachGenericRequirement" : true,
14+
"lineLength" : 100,
15+
"maximumBlankLines" : 1,
16+
"multiElementCollectionTrailingCommas" : true,
17+
"noAssignmentInExpressions" : {
18+
"allowedFunctions" : []
19+
},
20+
"prioritizeKeepingFunctionOutputTogether" : true,
21+
"respectsExistingLineBreaks" : false,
22+
"rules" : {
23+
"AllPublicDeclarationsHaveDocumentation" : false,
24+
"AlwaysUseLiteralForEmptyCollectionInit" : false,
25+
"AlwaysUseLowerCamelCase" : true,
26+
"AmbiguousTrailingClosureOverload" : true,
27+
"BeginDocumentationCommentWithOneLineSummary" : false,
28+
"DoNotUseSemicolons" : true,
29+
"DontRepeatTypeInStaticProperties" : true,
30+
"FileScopedDeclarationPrivacy" : true,
31+
"FullyIndirectEnum" : true,
32+
"GroupNumericLiterals" : true,
33+
"IdentifiersMustBeASCII" : true,
34+
"NeverForceUnwrap" : false,
35+
"NeverUseForceTry" : false,
36+
"NeverUseImplicitlyUnwrappedOptionals" : false,
37+
"NoAccessLevelOnExtensionDeclaration" : true,
38+
"NoAssignmentInExpressions" : true,
39+
"NoBlockComments" : true,
40+
"NoCasesWithOnlyFallthrough" : true,
41+
"NoEmptyTrailingClosureParentheses" : true,
42+
"NoLabelsInCasePatterns" : true,
43+
"NoLeadingUnderscores" : true,
44+
"NoParensAroundConditions" : true,
45+
"NoPlaygroundLiterals" : true,
46+
"NoVoidReturnOnFunctionSignature" : true,
47+
"OmitExplicitReturns" : true,
48+
"OneCasePerLine" : false,
49+
"OneVariableDeclarationPerLine" : false,
50+
"OnlyOneTrailingClosureArgument" : true,
51+
"OrderedImports" : true,
52+
"ReplaceForEachWithForLoop" : true,
53+
"ReturnVoidInsteadOfEmptyTuple" : true,
54+
"TypeNamesShouldBeCapitalized" : true,
55+
"UseEarlyExits" : true,
56+
"UseLetInEveryBoundCaseVariable" : true,
57+
"UseShorthandTypeNames" : true,
58+
"UseSingleLinePropertyGetter" : true,
59+
"UseSynthesizedInitializer" : true,
60+
"UseTripleSlashForDocumentationComments" : true,
61+
"UseWhereClausesInForLoops" : true,
62+
"ValidateDocumentationComments" : true
63+
},
64+
"spacesAroundRangeFormationOperators" : true,
65+
"tabWidth" : 8,
66+
"version" : 1
67+
}

Diff for: Contributing.md

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
## Introduction
2+
3+
Welcome to Swift JSON Schema! Contributions are welcome and greatly appreciated. By contributing, you are helping to make this project better for everyone.
4+
5+
## Style Guidelines
6+
7+
- Follow the [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/).
8+
- Write clear and concise comments where necessary.
9+
- Ensure your code is well-documented and includes meaningful test cases.
10+
11+
## Code Formatting
12+
13+
All code must be formatted using Swift format to ensure consistency across the codebase. The CI pipeline will check for formatting issues automatically. You can add the `auto-format` label to your pull requests to enable automatic Swift formatting before merging.
14+
15+
### How to Use
16+
17+
1. Create a pull request as usual.
18+
2. Add the `auto-format` label to your pull request.
19+
20+
When the label is added, the CI pipeline will automatically format the code and commit the changes to your pull request.
21+
22+
For more information about Swift format, visit the [Swift format repository](https://github.com/apple/swift-format).

Diff for: Sources/JSONResultBuilders/JSONSchemaBuilders.swift

+12-14
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,22 @@
11
import JSONSchema
22

3-
@resultBuilder
4-
public struct JSONSchemaBuilder {
3+
@resultBuilder public struct JSONSchemaBuilder {
54
public static func buildBlock(_ expression: JSONSchemaRepresentable) -> JSONSchemaRepresentable {
65
expression
76
}
87

9-
public static func buildBlock(_ components: JSONSchemaRepresentable...) -> [JSONSchemaRepresentable] {
10-
components
11-
}
8+
public static func buildBlock(
9+
_ components: JSONSchemaRepresentable...
10+
) -> [JSONSchemaRepresentable] { components }
1211

13-
public static func buildBlock(_ components: [JSONSchemaRepresentable]) -> [JSONSchemaRepresentable] {
14-
components
15-
}
12+
public static func buildBlock(
13+
_ components: [JSONSchemaRepresentable]
14+
) -> [JSONSchemaRepresentable] { components }
1615

1716
// MARK: Advanced builers
1817

19-
public static func buildOptional(_ component: JSONSchemaRepresentable?) -> JSONSchemaRepresentable {
20-
component ?? JSONNull()
21-
}
18+
public static func buildOptional(_ component: JSONSchemaRepresentable?) -> JSONSchemaRepresentable
19+
{ component ?? JSONNull() }
2220

2321
public static func buildEither(first: JSONSchemaRepresentable) -> JSONSchemaRepresentable {
2422
first
@@ -28,7 +26,7 @@ public struct JSONSchemaBuilder {
2826
second
2927
}
3028

31-
public static func buildArray(_ components: [JSONSchemaRepresentable]) -> [JSONSchemaRepresentable] {
32-
components
33-
}
29+
public static func buildArray(
30+
_ components: [JSONSchemaRepresentable]
31+
) -> [JSONSchemaRepresentable] { components }
3432
}

Diff for: Sources/JSONResultBuilders/JSONSchemaRepresentable+Annotations.swift

+10-10
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,49 @@
11
import JSONSchema
22

3-
public extension JSONSchemaRepresentable {
4-
func title(_ value: String) -> Self {
3+
extension JSONSchemaRepresentable {
4+
public func title(_ value: String) -> Self {
55
var copy = self
66
copy.annotations.title = value
77
return copy
88
}
99

10-
func description(_ value: String) -> Self {
10+
public func description(_ value: String) -> Self {
1111
var copy = self
1212
copy.annotations.description = value
1313
return copy
1414
}
1515

16-
func `default`(@JSONValueBuilder _ value: () -> JSONValueRepresentable) -> Self {
16+
public func `default`(@JSONValueBuilder _ value: () -> JSONValueRepresentable) -> Self {
1717
var copy = self
1818
copy.annotations.default = value().value
1919
return copy
2020
}
2121

22-
func examples(@JSONValueBuilder _ examples: () -> JSONValueRepresentable) -> Self {
22+
public func examples(@JSONValueBuilder _ examples: () -> JSONValueRepresentable) -> Self {
2323
var copy = self
2424
copy.annotations.examples = examples().value
2525
return copy
2626
}
27-
28-
func readOnly(_ value: Bool) -> Self {
27+
28+
public func readOnly(_ value: Bool) -> Self {
2929
var copy = self
3030
copy.annotations.readOnly = value
3131
return copy
3232
}
3333

34-
func writeOnly(_ value: Bool) -> Self {
34+
public func writeOnly(_ value: Bool) -> Self {
3535
var copy = self
3636
copy.annotations.writeOnly = value
3737
return copy
3838
}
3939

40-
func deprecated(_ value: Bool) -> Self {
40+
public func deprecated(_ value: Bool) -> Self {
4141
var copy = self
4242
copy.annotations.deprecated = value
4343
return copy
4444
}
4545

46-
func comment(_ value: String) -> Self {
46+
public func comment(_ value: String) -> Self {
4747
var copy = self
4848
copy.annotations.comment = value
4949
return copy

Diff for: Sources/JSONResultBuilders/JSONValueBuilders.swift

+12-17
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
@resultBuilder
2-
public struct JSONValueBuilder {
3-
public static func buildExpression(_ expression: JSONValueRepresentable?) -> JSONValueRepresentable {
4-
expression ?? JSONNullValue()
5-
}
1+
@resultBuilder public struct JSONValueBuilder {
2+
public static func buildExpression(
3+
_ expression: JSONValueRepresentable?
4+
) -> JSONValueRepresentable { expression ?? JSONNullValue() }
65

76
public static func buildBlock(_ expression: JSONValueRepresentable...) -> JSONValueRepresentable {
8-
if expression.count == 1 {
9-
expression[0]
10-
} else {
11-
JSONArrayValue(elements: expression)
12-
}
7+
if expression.count == 1 { expression[0] } else { JSONArrayValue(elements: expression) }
138
}
149

1510
// MARK: Type hints
@@ -34,9 +29,9 @@ public struct JSONValueBuilder {
3429
JSONArrayValue(elements: expression)
3530
}
3631

37-
public static func buildExpression(_ expression: [String: JSONValueRepresentable]) -> JSONObjectValue {
38-
JSONObjectValue(properties: expression)
39-
}
32+
public static func buildExpression(
33+
_ expression: [String: JSONValueRepresentable]
34+
) -> JSONObjectValue { JSONObjectValue(properties: expression) }
4035

4136
// MARK: Additional array type hints
4237

@@ -78,7 +73,9 @@ public struct JSONValueBuilder {
7873
JSONObjectValue(properties: expression.mapValues { JSONStringValue(string: $0) })
7974
}
8075

81-
public static func buildExpression(_ expression: [String: JSONValueRepresentable?]) -> JSONObjectValue {
76+
public static func buildExpression(
77+
_ expression: [String: JSONValueRepresentable?]
78+
) -> JSONObjectValue {
8279
JSONObjectValue(properties: expression.mapValues { $0 ?? JSONNullValue() })
8380
}
8481

@@ -92,9 +89,7 @@ public struct JSONValueBuilder {
9289
component ?? JSONNullValue()
9390
}
9491

95-
public static func buildEither(first: JSONValueRepresentable) -> JSONValueRepresentable {
96-
first
97-
}
92+
public static func buildEither(first: JSONValueRepresentable) -> JSONValueRepresentable { first }
9893

9994
public static func buildEither(second: JSONValueRepresentable) -> JSONValueRepresentable {
10095
second
+1-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
import JSONSchema
22

3-
public protocol JSONValueRepresentable {
4-
var value: JSONValue { get }
5-
}
3+
public protocol JSONValueRepresentable { var value: JSONValue { get } }

Diff for: Sources/JSONResultBuilders/Property/JSONPropertyBuilders.swift

+6-16
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,13 @@
1-
@resultBuilder
2-
public struct JSONPropertySchemaBuilder {
1+
@resultBuilder public struct JSONPropertySchemaBuilder {
32
public static func buildBlock(_ components: [JSONProperty]...) -> [JSONProperty] {
43
components.flatMap { $0 }
54
}
65

7-
public static func buildBlock(_ components: JSONProperty...) -> [JSONProperty] {
8-
components
9-
}
6+
public static func buildBlock(_ components: JSONProperty...) -> [JSONProperty] { components }
107

11-
public static func buildEither(first component: [JSONProperty]) -> [JSONProperty] {
12-
component
13-
}
8+
public static func buildEither(first component: [JSONProperty]) -> [JSONProperty] { component }
149

15-
public static func buildEither(second component: [JSONProperty]) -> [JSONProperty] {
16-
component
17-
}
10+
public static func buildEither(second component: [JSONProperty]) -> [JSONProperty] { component }
1811

1912
public static func buildOptional(_ component: [JSONProperty]?) -> [JSONProperty] {
2013
component ?? []
@@ -37,8 +30,7 @@ extension JSONObject {
3730
}
3831
}
3932

40-
@resultBuilder
41-
public struct JSONPropertyBuilder {
33+
@resultBuilder public struct JSONPropertyBuilder {
4234
public static func buildBlock(_ components: [JSONPropertyValue]...) -> [JSONPropertyValue] {
4335
components.flatMap { $0 }
4436
}
@@ -67,8 +59,6 @@ public struct JSONPropertyBuilder {
6759
extension JSONObjectValue {
6860
public init(@JSONPropertyBuilder _ content: () -> [JSONPropertyValue]) {
6961
self.properties = content()
70-
.reduce(into: [:]) { partialResult, property in
71-
partialResult[property.key] = property.value
72-
}
62+
.reduce(into: [:]) { partialResult, property in partialResult[property.key] = property.value }
7363
}
7464
}

0 commit comments

Comments
 (0)