Skip to content
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

Support any suffix and add standard yaml to MediaType #53

Merged
merged 5 commits into from
Nov 2, 2024
Merged
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
31 changes: 18 additions & 13 deletions .github/workflows/build-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,33 @@ on:
jobs:

check-build-test:
runs-on: macos-12
runs-on: macos-15

steps:

- name: Install Tools
run: |
brew tap a7ex/homebrew-formulae
brew install xcbeautify sonar-scanner findsimulator
brew install swiftlint sonar-scanner
env:
HOMEBREW_NO_INSTALL_CLEANUP: 1

- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_14.1.app/Contents/Developer
run: sudo xcode-select -s /Applications/Xcode_16.1.app/Contents/Developer

- uses: actions/cache@v4
with:
path: .build
key: ${{ runner.os }}-${{ env.MD_APPLE_SDK_ROOT }}-spm-${{ hashFiles('./Package.resolved') }}
restore-keys: |
${{ runner.os }}-${{ env.MD_APPLE_SDK_ROOT }}-spm-

- name: SwiftLint
run: |
mkdir .build
mkdir -p .build
swiftlint lint --reporter json > .build/swiftlint.json

- name: Build/Test
Expand All @@ -39,7 +45,7 @@ jobs:
- name: Convert Coverage
run: xcrun llvm-cov show -instr-profile=.build/debug/codecov/default.profdata .build/debug/SundayPackageTests.xctest/Contents/MacOS/SundayPackageTests > .build/coverage.report

- name: Sonar Scanner
- name: SonarCloud Scan
run: sonar-scanner
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -48,7 +54,7 @@ jobs:

build-test:

runs-on: macos-12
runs-on: macos-15

needs: [check-build-test]

Expand All @@ -60,21 +66,20 @@ jobs:

- name: Install Tools
run: |
brew tap a7ex/homebrew-formulae
brew install xcbeautify findsimulator
brew install xcbeautify
env:
HOMEBREW_NO_INSTALL_CLEANUP: 1

- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_14.1.app/Contents/Developer
run: sudo xcode-select -s /Applications/Xcode_16.1.app/Contents/Developer

- name: Build/Test
run: make build-test-${{ matrix.platform }}

- name: Report Test Results
uses: kishikawakatsumi/xcresulttool@v1
uses: slidoapp/xcresulttool@main
if: always()
with:
title: Test Results ${{ matrix.platform }}
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/publish-docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:

build:

runs-on: macos-12
runs-on: macos-15

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_14.1.app/Contents/Developer
run: sudo xcode-select -s /Applications/Xcode_16.1.app/Contents/Developer

- run: mkdir -p docs

Expand All @@ -39,7 +39,7 @@ jobs:
run: tar -czf docs.tar.gz docs

- name: Upload Docs Archive
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: docs-${{ github.ref_name }}
path: docs.tar.gz
Expand All @@ -50,10 +50,10 @@ jobs:
needs: [ build ]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Download Docs Archive
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: docs-${{ github.ref_name }}
path: '.'
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/publish-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ jobs:

build:

runs-on: macos-12
runs-on: macos-15

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Select Xcode
run: sudo xcode-select -s /Applications/Xcode_14.1.app/Contents/Developer
run: sudo xcode-select -s /Applications/Xcode_16.1.app/Contents/Developer

- run: mkdir -p docs

Expand All @@ -39,7 +39,7 @@ jobs:
run: tar -czf docs.tar.gz docs

- name: Upload Docs Archive
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: docs-${{ github.ref_name }}
path: docs.tar.gz
Expand All @@ -50,10 +50,10 @@ jobs:
needs: [ build ]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- name: Download Docs Archive
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: docs-${{ github.ref_name }}
path: '.'
Expand All @@ -69,10 +69,10 @@ jobs:

- name: Check Version Format in Tag
id: version
uses: nowsprinting/check-version-format-action@v3
uses: nowsprinting/check-version-format-action@v4

- name: Create GitHub Release
uses: softprops/action-gh-release@v1
uses: softprops/action-gh-release@v2
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
Expand Down
19 changes: 11 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,10 @@ comma:=,
default: clean build-test-all

check-tools:
@which findsimulator || (echo "findsimulator is required. run 'make install-tools'" && exit 1)
@which xcbeautify || (echo "xcbeautify is required. run 'make install-tools'" && exit 1)

install-tools:
brew tap a7ex/homebrew-formulae
brew install xcbeautify findsimulator
brew install xcbeautify

clean:
@rm -rf TestResults
Expand All @@ -20,25 +18,30 @@ make-test-results-dir:
mkdir -p TestResults

define buildtest
rm -rf ./TestResults/$(1)
set -o pipefail && \
xcodebuild -scheme $(project)-Package \
-derivedDataPath .derived-data/$(1) -resultBundleVersion 3 -resultBundlePath ./TestResults/$(1) -destination '$(2)' \
-enableCodeCoverage=YES -enableAddressSanitizer=YES -enableThreadSanitizer=YES -enableUndefinedBehaviorSanitizer=YES \
-clonedSourcePackagesDirPath ${HOME}/.xcode-pkgs \
-clonedSourcePackagesDirPath ${PWD}/.xcode-pkgs -packageCachePath ${PWD}/.xcode-pkgs/_cache_ \
-skipMacroValidation \
test | xcbeautify
endef

build-test-macos: check-tools
$(call buildtest,macOS,platform=macOS)
$(call buildtest,macos,platform=macOS)

build-test-ios: check-tools
$(call buildtest,iOS,$(shell findsimulator --os-type ios "iPhone"))
$(call buildtest,ios,platform=iOS Simulator$(comma)name=iPhone 16)

build-test-tvos: check-tools
$(call buildtest,tvOS,$(shell findsimulator --os-type tvos "Apple TV"))
$(call buildtest,tvos,platform=tvOS Simulator$(comma)name=Apple TV)

build-test-watchos: check-tools
$(call buildtest,watchOS,$(shell findsimulator --os-type watchos "Apple Watch"))
$(call buildtest,watchos,platform=watchOS Simulator$(comma)name=Apple Watch Series 10 (46mm))

build-test-visionos: check-tools
$(call buildtest,visionos,platform=visionOS Simulator$(comma)name=Apple Vision Pro)

build-test-all: build-test-macos build-test-ios build-test-tvos build-test-watchos

Expand Down
43 changes: 31 additions & 12 deletions Sources/Sunday/MediaType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,33 @@ public struct MediaType {
case any = "*"
}

public enum Suffix: String, CaseIterable, Equatable, Hashable, Codable {
case xml
case json
case ber
case der
case fastinfoset
case wbxml
case zip
case cbor
public struct Suffix: RawRepresentable, Equatable, Hashable, Codable, ExpressibleByStringLiteral {
public var rawValue: String

public init(rawValue: String) {
self.rawValue = rawValue
}

public init(_ value: String) {
self.rawValue = value
}

public init(stringLiteral value: StringLiteralType) {
self.rawValue = value
}

var value: String { rawValue }

public static func suffix(_ suffix: String) -> Suffix { Suffix(rawValue: suffix) }

public static let xml = suffix("xml")
public static let json = suffix("json")
public static let ber = suffix("ber")
public static let der = suffix("der")
public static let fastinfoset = suffix("fastinfoset")
public static let wbxml = suffix("wbxml")
public static let zip = suffix("zip")
public static let cbor = suffix("cbor")
}

public let type: `Type`
Expand Down Expand Up @@ -128,8 +146,8 @@ public struct MediaType {
}
self.subtype = subtype

if let suffixVal = match.captures[3]?.lowercased(), let suffix = Suffix(rawValue: suffixVal) {
self.suffix = suffix
if let suffixVal = match.captures[3]?.lowercased() {
self.suffix = .init(suffixVal)
}
else {
suffix = nil
Expand Down Expand Up @@ -182,14 +200,15 @@ public struct MediaType {
public var value: String {
let type = self.type.rawValue
let tree = self.tree.rawValue
let suffix = self.suffix != nil ? "+\(self.suffix!.rawValue)" : ""
let suffix = self.suffix.map { "+\($0.value)" } ?? ""
let parameters = self.parameters.keys.sorted().map { key in ";\(key)=\(self.parameters[key]!)" }.joined()
return "\(type)/\(tree)\(subtype)\(suffix)\(parameters)"
}

public static let plain = MediaType(type: .text, subtype: "plain")
public static let html = MediaType(type: .text, subtype: "html")
public static let json = MediaType(type: .application, subtype: "json")
public static let yaml = MediaType(type: .application, subtype: "json")
public static let cbor = MediaType(type: .application, subtype: "cbor")
public static let eventStream = MediaType(type: .text, subtype: "event-stream")
public static let octetStream = MediaType(type: .application, subtype: "octet-stream")
Expand Down
20 changes: 18 additions & 2 deletions Sources/SundayServer/HTTPResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public protocol HTTPResponse: AnyObject {
/// - Precondition: state == .sendingBody
/// - Postcondition: state == .sendingChunks
/// - Parameters:
/// - data: Data of chunk
/// - chunk: Data of chunk
///
func send(chunk: Data)

Expand Down Expand Up @@ -133,7 +133,7 @@ public protocol HTTPResponse: AnyObject {
}


extension HTTPResponse {
public extension HTTPResponse {

func header(forName name: String) -> String? {
return headers(forName: name).first
Expand Down Expand Up @@ -238,4 +238,20 @@ extension HTTPResponse {
send(status: .init(code: statusCode), headers: headers, value: value)
}

/// Sends a chunk of response data, as the UTF8 encoded string.
///
/// - Precondition: state == .sendingBody
/// - Postcondition: state == .sendingChunks
/// - Parameters:
/// - chunk: String of chunk
/// - as: Encoding for string
///
func send(chunk: String) {
return send(chunk: Data(chunk.utf8))
}

}

public enum EncodingError: String, Error {
case unableToEncodeString
}
Loading
Loading