Skip to content

Commit

Permalink
Merge pull request #74 from outfoxx/feature/yaml-schemas
Browse files Browse the repository at this point in the history
Implement YAML schemas for properly reading and writing scalars
  • Loading branch information
kdubb authored Jun 10, 2024
2 parents 7752668 + 7046ec7 commit 660e33e
Show file tree
Hide file tree
Showing 13 changed files with 341 additions and 123 deletions.
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,15 @@
"version": "1.1.1"
}
},
{
"package": "Regex",
"repositoryURL": "https://github.com/sharplet/Regex.git",
"state": {
"branch": null,
"revision": "76c2b73d4281d77fc3118391877efd1bf972f515",
"version": "2.1.1"
}
},
{
"package": "swift-collections",
"repositoryURL": "https://github.com/apple/swift-collections.git",
Expand Down
10 changes: 7 additions & 3 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ import PackageDescription
let pcDeps: [Target.Dependency] = [
"BigInt",
.product(name: "Collections", package: "swift-collections"),
.byName(name: "Float16", condition: .when(platforms: [.macOS, .macCatalyst, .linux]))
.byName(name: "Float16", condition: .when(platforms: [.macOS, .macCatalyst, .linux])),
.product(name: "Regex", package: "regex"),
]
#else
let pcDeps: [Target.Dependency] = [
"BigInt",
.product(name: "Collections", package: "swift-collections"),
"Float16",
.product(name: "Regex", package: "regex"),
]
#endif

Expand All @@ -43,7 +45,8 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/attaswift/BigInt.git", .upToNextMinor(from: "5.3.0")),
.package(url: "https://github.com/SusanDoggie/Float16.git", .upToNextMinor(from: "1.1.1")),
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMinor(from: "1.0.4"))
.package(url: "https://github.com/apple/swift-collections.git", .upToNextMinor(from: "1.0.4")),
.package(url: "https://github.com/sharplet/Regex.git", .upToNextMinor(from: "2.1.1"))
],
targets: [
.target(
Expand Down Expand Up @@ -100,7 +103,8 @@ let package = Package(
"PotentCodables",
"BigInt",
"Cfyaml",
.product(name: "Collections", package: "swift-collections")
.product(name: "Collections", package: "swift-collections"),
.product(name: "Regex", package: "regex")
]
),
.testTarget(
Expand Down
34 changes: 34 additions & 0 deletions Sources/PotentCodables/Regexes.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//
// Pattern.swift
// PotentCodables
//
// Copyright © 2019 Outfox, inc.
//
//
// Distributed under the MIT License, See LICENSE for details.
//

import Foundation
import Regex


extension Regex: ExpressibleByStringLiteral {

public init(stringLiteral value: StaticString) {
self.init(value)
}

}

extension Regex {

public static func anyOf(_ regexes: Regex...) -> Regex {
do {
return try Regex(string: regexes.map { "(\($0.description))" }.joined(separator: "|"))
}
catch {
preconditionFailure("unexpected error creating regex from joined set: \(error)")
}
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Cfyaml.swift
// Libfyaml.swift
// PotentCodables
//
// Copyright © 2021 Outfox, inc.
Expand All @@ -11,7 +11,7 @@
import Cfyaml


enum Libfyaml {
internal enum Libfyaml {

internal static func createParser() -> OpaquePointer? {

Expand Down Expand Up @@ -91,6 +91,28 @@ enum Libfyaml {
return diag
}

internal static func hasError(in diag: OpaquePointer?) -> Bool {

guard let diag else {
return true
}

return fy_diag_got_error(diag)
}

internal static func error(from diag: OpaquePointer?) -> (message: String, line: Int, column: Int)? {

guard let diag else {
return nil
}

var prev: UnsafeMutableRawPointer?
guard let error = fy_diag_errors_iterate(diag, &prev) else {
return nil
}

return (String(cString: error.pointee.msg), Int(error.pointee.line), Int(error.pointee.column))
}

}

Expand Down
8 changes: 6 additions & 2 deletions Sources/PotentYAML/YAML.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public enum YAML {
self.isNegative = isNegative
}

public init(_ value: String) {
self.init(value, isInteger: value.allSatisfy { $0.isNumber || $0 == "-" }, isNegative: value.hasPrefix("-"))
public init(_ value: String, schema: YAMLSchema = .core) {
self.init(value, isInteger: schema.isInt(value), isNegative: value.hasPrefix("-"))
}

public init<T: FloatingPoint>(_ value: T) {
Expand Down Expand Up @@ -193,6 +193,10 @@ public enum YAML {
case doubleQuoted = 2
case literal = 3
case folded = 4

public var isQuoted: Bool {
self == .singleQuoted || self == .doubleQuoted
}
}

public enum CollectionStyle: Int32 {
Expand Down
Loading

0 comments on commit 660e33e

Please sign in to comment.