Skip to content

Implement YAML schemas for properly reading and writing scalars #74

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 2 commits into from
Jun 10, 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
9 changes: 9 additions & 0 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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