Skip to content

Add Swift Format #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 18, 2024
18 changes: 18 additions & 0 deletions .github/pull_request_template.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## Description

Please include a summary of the changes and the related issue. Please also include relevant motivation and context.

## Type of Change

- [ ] Bug fix
- [ ] New feature
- [ ] Breaking change
- [ ] Documentation update

## Additional Notes

Add any other context or screenshots about the pull request here.

---

**Note:** You can add the `auto-format` label to this pull request to enable automatic Swift formatting.
2 changes: 1 addition & 1 deletion .github/workflows/swift.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This workflow will build a Swift project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-swift

name: Swift
name: CI

on:
push:
Expand Down
60 changes: 60 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
name: Format

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
format:
runs-on: macos-latest

permissions:
contents: write

name: Lint and format
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.PAT }}
ref: ${{ github.head_ref }}

- name: Install swift-format
run: brew install swift-format

- name: Format
run: swift-format format --in-place --parallel --recursive Sources/ Tests/

- name: Check for formatting changes
id: check_changes
run: |
git diff --exit-code
continue-on-error: true

- name: Check if PR has a specific label
id: check_label
uses: actions/github-script@v6
with:
result-encoding: string
script: |
const labelToCheck = 'auto-format';
const prLabels = context.payload.pull_request.labels.map(label => label.name);
return prLabels.includes(labelToCheck);

- name: Apply auto-formatting
if: steps.check_changes.outcome == 'failure' && steps.check_label.outputs.result == 'true'
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: Apply auto-formatting

- name: Fail if changes detected (optional)
if: steps.check_changes.outcome == 'failure' && steps.check_label.outputs.result == 'false'
run: |
echo "::error::Formatting issues detected. Please fix them."
exit 1

- name: Lint
id: lint
run: |
swift-format lint --strict --parallel --recursive Sources/ Tests/
67 changes: 67 additions & 0 deletions .swift-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
{
"fileScopedDeclarationPrivacy" : {
"accessLevel" : "private"
},
"indentConditionalCompilationBlocks" : true,
"indentSwitchCaseLabels" : false,
"indentation" : {
"spaces" : 2
},
"lineBreakAroundMultilineExpressionChainComponents" : true,
"lineBreakBeforeControlFlowKeywords" : false,
"lineBreakBeforeEachArgument" : true,
"lineBreakBeforeEachGenericRequirement" : true,
"lineLength" : 100,
"maximumBlankLines" : 1,
"multiElementCollectionTrailingCommas" : true,
"noAssignmentInExpressions" : {
"allowedFunctions" : []
},
"prioritizeKeepingFunctionOutputTogether" : true,
"respectsExistingLineBreaks" : false,
"rules" : {
"AllPublicDeclarationsHaveDocumentation" : false,
"AlwaysUseLiteralForEmptyCollectionInit" : false,
"AlwaysUseLowerCamelCase" : true,
"AmbiguousTrailingClosureOverload" : true,
"BeginDocumentationCommentWithOneLineSummary" : false,
"DoNotUseSemicolons" : true,
"DontRepeatTypeInStaticProperties" : true,
"FileScopedDeclarationPrivacy" : true,
"FullyIndirectEnum" : true,
"GroupNumericLiterals" : true,
"IdentifiersMustBeASCII" : true,
"NeverForceUnwrap" : false,
"NeverUseForceTry" : false,
"NeverUseImplicitlyUnwrappedOptionals" : false,
"NoAccessLevelOnExtensionDeclaration" : true,
"NoAssignmentInExpressions" : true,
"NoBlockComments" : true,
"NoCasesWithOnlyFallthrough" : true,
"NoEmptyTrailingClosureParentheses" : true,
"NoLabelsInCasePatterns" : true,
"NoLeadingUnderscores" : true,
"NoParensAroundConditions" : true,
"NoPlaygroundLiterals" : true,
"NoVoidReturnOnFunctionSignature" : true,
"OmitExplicitReturns" : true,
"OneCasePerLine" : false,
"OneVariableDeclarationPerLine" : false,
"OnlyOneTrailingClosureArgument" : true,
"OrderedImports" : true,
"ReplaceForEachWithForLoop" : true,
"ReturnVoidInsteadOfEmptyTuple" : true,
"TypeNamesShouldBeCapitalized" : true,
"UseEarlyExits" : true,
"UseLetInEveryBoundCaseVariable" : true,
"UseShorthandTypeNames" : true,
"UseSingleLinePropertyGetter" : true,
"UseSynthesizedInitializer" : true,
"UseTripleSlashForDocumentationComments" : true,
"UseWhereClausesInForLoops" : true,
"ValidateDocumentationComments" : true
},
"spacesAroundRangeFormationOperators" : true,
"tabWidth" : 8,
"version" : 1
}
22 changes: 22 additions & 0 deletions Contributing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
## Introduction

Welcome to Swift JSON Schema! Contributions are welcome and greatly appreciated. By contributing, you are helping to make this project better for everyone.

## Style Guidelines

- Follow the [Swift API Design Guidelines](https://swift.org/documentation/api-design-guidelines/).
- Write clear and concise comments where necessary.
- Ensure your code is well-documented and includes meaningful test cases.

## Code Formatting

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.

### How to Use

1. Create a pull request as usual.
2. Add the `auto-format` label to your pull request.

When the label is added, the CI pipeline will automatically format the code and commit the changes to your pull request.

For more information about Swift format, visit the [Swift format repository](https://github.com/apple/swift-format).
26 changes: 12 additions & 14 deletions Sources/JSONResultBuilders/JSONSchemaBuilders.swift
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
import JSONSchema

@resultBuilder
public struct JSONSchemaBuilder {
@resultBuilder public struct JSONSchemaBuilder {
public static func buildBlock(_ expression: JSONSchemaRepresentable) -> JSONSchemaRepresentable {
expression
}

public static func buildBlock(_ components: JSONSchemaRepresentable...) -> [JSONSchemaRepresentable] {
components
}
public static func buildBlock(
_ components: JSONSchemaRepresentable...
) -> [JSONSchemaRepresentable] { components }

public static func buildBlock(_ components: [JSONSchemaRepresentable]) -> [JSONSchemaRepresentable] {
components
}
public static func buildBlock(
_ components: [JSONSchemaRepresentable]
) -> [JSONSchemaRepresentable] { components }

// MARK: Advanced builers

public static func buildOptional(_ component: JSONSchemaRepresentable?) -> JSONSchemaRepresentable {
component ?? JSONNull()
}
public static func buildOptional(_ component: JSONSchemaRepresentable?) -> JSONSchemaRepresentable
{ component ?? JSONNull() }

public static func buildEither(first: JSONSchemaRepresentable) -> JSONSchemaRepresentable {
first
Expand All @@ -28,7 +26,7 @@ public struct JSONSchemaBuilder {
second
}

public static func buildArray(_ components: [JSONSchemaRepresentable]) -> [JSONSchemaRepresentable] {
components
}
public static func buildArray(
_ components: [JSONSchemaRepresentable]
) -> [JSONSchemaRepresentable] { components }
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
import JSONSchema

public extension JSONSchemaRepresentable {
func title(_ value: String) -> Self {
extension JSONSchemaRepresentable {
public func title(_ value: String) -> Self {
var copy = self
copy.annotations.title = value
return copy
}

func description(_ value: String) -> Self {
public func description(_ value: String) -> Self {
var copy = self
copy.annotations.description = value
return copy
}

func `default`(@JSONValueBuilder _ value: () -> JSONValueRepresentable) -> Self {
public func `default`(@JSONValueBuilder _ value: () -> JSONValueRepresentable) -> Self {
var copy = self
copy.annotations.default = value().value
return copy
}

func examples(@JSONValueBuilder _ examples: () -> JSONValueRepresentable) -> Self {
public func examples(@JSONValueBuilder _ examples: () -> JSONValueRepresentable) -> Self {
var copy = self
copy.annotations.examples = examples().value
return copy
}
func readOnly(_ value: Bool) -> Self {

public func readOnly(_ value: Bool) -> Self {
var copy = self
copy.annotations.readOnly = value
return copy
}

func writeOnly(_ value: Bool) -> Self {
public func writeOnly(_ value: Bool) -> Self {
var copy = self
copy.annotations.writeOnly = value
return copy
}

func deprecated(_ value: Bool) -> Self {
public func deprecated(_ value: Bool) -> Self {
var copy = self
copy.annotations.deprecated = value
return copy
}

func comment(_ value: String) -> Self {
public func comment(_ value: String) -> Self {
var copy = self
copy.annotations.comment = value
return copy
Expand Down
29 changes: 12 additions & 17 deletions Sources/JSONResultBuilders/JSONValueBuilders.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
@resultBuilder
public struct JSONValueBuilder {
public static func buildExpression(_ expression: JSONValueRepresentable?) -> JSONValueRepresentable {
expression ?? JSONNullValue()
}
@resultBuilder public struct JSONValueBuilder {
public static func buildExpression(
_ expression: JSONValueRepresentable?
) -> JSONValueRepresentable { expression ?? JSONNullValue() }

public static func buildBlock(_ expression: JSONValueRepresentable...) -> JSONValueRepresentable {
if expression.count == 1 {
expression[0]
} else {
JSONArrayValue(elements: expression)
}
if expression.count == 1 { expression[0] } else { JSONArrayValue(elements: expression) }
}

// MARK: Type hints
Expand All @@ -34,9 +29,9 @@ public struct JSONValueBuilder {
JSONArrayValue(elements: expression)
}

public static func buildExpression(_ expression: [String: JSONValueRepresentable]) -> JSONObjectValue {
JSONObjectValue(properties: expression)
}
public static func buildExpression(
_ expression: [String: JSONValueRepresentable]
) -> JSONObjectValue { JSONObjectValue(properties: expression) }

// MARK: Additional array type hints

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

public static func buildExpression(_ expression: [String: JSONValueRepresentable?]) -> JSONObjectValue {
public static func buildExpression(
_ expression: [String: JSONValueRepresentable?]
) -> JSONObjectValue {
JSONObjectValue(properties: expression.mapValues { $0 ?? JSONNullValue() })
}

Expand All @@ -92,9 +89,7 @@ public struct JSONValueBuilder {
component ?? JSONNullValue()
}

public static func buildEither(first: JSONValueRepresentable) -> JSONValueRepresentable {
first
}
public static func buildEither(first: JSONValueRepresentable) -> JSONValueRepresentable { first }

public static func buildEither(second: JSONValueRepresentable) -> JSONValueRepresentable {
second
Expand Down
4 changes: 1 addition & 3 deletions Sources/JSONResultBuilders/JSONValueRepresentable.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import JSONSchema

public protocol JSONValueRepresentable {
var value: JSONValue { get }
}
public protocol JSONValueRepresentable { var value: JSONValue { get } }
22 changes: 6 additions & 16 deletions Sources/JSONResultBuilders/Property/JSONPropertyBuilders.swift
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
@resultBuilder
public struct JSONPropertySchemaBuilder {
@resultBuilder public struct JSONPropertySchemaBuilder {
public static func buildBlock(_ components: [JSONProperty]...) -> [JSONProperty] {
components.flatMap { $0 }
}

public static func buildBlock(_ components: JSONProperty...) -> [JSONProperty] {
components
}
public static func buildBlock(_ components: JSONProperty...) -> [JSONProperty] { components }

public static func buildEither(first component: [JSONProperty]) -> [JSONProperty] {
component
}
public static func buildEither(first component: [JSONProperty]) -> [JSONProperty] { component }

public static func buildEither(second component: [JSONProperty]) -> [JSONProperty] {
component
}
public static func buildEither(second component: [JSONProperty]) -> [JSONProperty] { component }

public static func buildOptional(_ component: [JSONProperty]?) -> [JSONProperty] {
component ?? []
Expand All @@ -37,8 +30,7 @@ extension JSONObject {
}
}

@resultBuilder
public struct JSONPropertyBuilder {
@resultBuilder public struct JSONPropertyBuilder {
public static func buildBlock(_ components: [JSONPropertyValue]...) -> [JSONPropertyValue] {
components.flatMap { $0 }
}
Expand Down Expand Up @@ -67,8 +59,6 @@ public struct JSONPropertyBuilder {
extension JSONObjectValue {
public init(@JSONPropertyBuilder _ content: () -> [JSONPropertyValue]) {
self.properties = content()
.reduce(into: [:]) { partialResult, property in
partialResult[property.key] = property.value
}
.reduce(into: [:]) { partialResult, property in partialResult[property.key] = property.value }
}
}
Loading
Loading