### Build Yams with CMake for Apple Platforms Source: https://github.com/jpsim/yams/blob/main/README.md Use this CMake command to build Yams for Apple platforms. Foundation is included in the SDK, so no separate directory is needed. ```bash cmake -B /path/to/build -G Ninja -S /path/to/yams -DCMAKE_BUILD_TYPE=Release cmake --build /path/to/build ``` -------------------------------- ### Customize YAML Emission Style in Swift Source: https://github.com/jpsim/yams/blob/main/Docs.md Demonstrates how to customize the emission style of a YAML sequence to use flow style (e.g., `[a, b, c]`) instead of the default block style. ```swift import Yams var node: Node = ["a", "b", "c"] node.sequence?.style = .flow dofor { let yamlString = try Yams.serialize(node: node) print(yamlString) } catch { print("handle error: \(error)") } // Prints: // [a, b, c] ``` -------------------------------- ### Serialize and Compose Yams.Node Types Source: https://github.com/jpsim/yams/blob/main/README.md Demonstrates serializing a Yams.Node with flow style to a YAML string and composing a Node from a YAML string. ```swift var map: Yams.Node = [ "array": [ 1, 2, 3 ] ] map.mapping?.style = .flow map["array"]?.sequence?.style = .flow let yaml = try Yams.serialize(node: map) yaml == """ {array: [1, 2, 3]} """ let node = try Yams.compose(yaml: yaml) map == node ``` -------------------------------- ### Build Yams with CMake for Non-Apple Platforms Source: https://github.com/jpsim/yams/blob/main/README.md Use this CMake command to build Yams for non-Apple platforms, specifying the build type and Foundation directory. ```bash cmake -B /path/to/build -G Ninja -S /path/to/yams -DCMAKE_BUILD_TYPE=Release -DFoundation_DIR=/path/to/foundation/build/cmake/modules cmake --build /path/to/build ``` -------------------------------- ### Configure YAMS CMake Files Source: https://github.com/jpsim/yams/blob/main/cmake/modules/CMakeLists.txt Configures the YamsConfig.cmake file from a template. This is typically used to set up project-specific configurations. ```cmake set(YAMS_EXPORTS_FILE ${CMAKE_CURRENT_BINARY_DIR}/YamsExports.cmake) configure_file(YamsConfig.cmake.in ${CMAKE_CURRENT_BINARY_DIR}/YamsConfig.cmake) ``` -------------------------------- ### YAML to NSMutableDictionary/NSMutableArray Conversion Source: https://github.com/jpsim/yams/blob/main/README.md Use a custom constructor to convert YAML into mutable Cocoa-style collections like NSMutableDictionary and NSMutableArray. ```swift let yaml = """ names: - Alice - Bob """ let constructor = Constructor(Constructor.defaultScalarMap, Constructor.nsMutableMappingMap, Constructor.nsMutableSequenceMap) let result = try Yams.load(yaml: yaml, .default, constructor) as? NSMutableDictionary let names = result?["names"] as? NSMutableArray print(names ?? "No data") // -> (Alice, Bob) ``` -------------------------------- ### Encode and Decode Swift Standard Library Types with Yams Source: https://github.com/jpsim/yams/blob/main/README.md Shows how to encode Swift standard library types like dictionaries, arrays, and strings to YAML and decode them back. ```swift // [String: Any] let dictionary: [String: Any] = ["key": "value"] let mapYAML: String = try Yams.dump(object: dictionary) mapYAML == """ key: value """ let loadedDictionary = try Yams.load(yaml: mapYAML) as? [String: Any] // [Any] let array: [Int] = [1, 2, 3] let sequenceYAML: String = try Yams.dump(object: array) sequenceYAML == """ - 1 - 2 - 3 """ let loadedArray: [Int]? = try Yams.load(yaml: sequenceYAML) as? [Int] // Any let string = "string" let scalarYAML: String = try Yams.dump(object: string) scalarYAML == """ string """ let loadedString: String? = try Yams.load(yaml: scalarYAML) as? String ``` -------------------------------- ### Emit YAML String from Swift Array Source: https://github.com/jpsim/yams/blob/main/Docs.md Serializes a Swift array of strings into a YAML formatted string. Includes basic error handling for the serialization process. ```swift import Yams dofor { let yamlString = try Yams.serialize(node: ["a", "b", "c"]) print(yamlString) } catch { print("handle error: \(error)") } // Prints: // - a // - b // - c ``` -------------------------------- ### Swift Standard Library Types Encoding and Decoding Source: https://github.com/jpsim/yams/blob/main/README.md Encode Swift Standard Library types (like Dictionary, Array, Any) to YAML strings and decode YAML strings into these types. ```APIDOC ## Swift Standard Library types ### Description Allows for encoding Swift Standard Library types into YAML and decoding YAML into these types. The type is inferred from the YAML content, which can have a larger computational overhead during decoding due to upfront type inference. ### Encoding - **Method:** `Yams.dump(object:) - **Description:** Produces a YAML `String` from an instance of Swift Standard Library types. ### Decoding - **Method:** `Yams.load(yaml:) - **Description:** Produces an instance of Swift Standard Library types as `Any` from a YAML `String`. ### Request Example (Encoding Dictionary) ```swift // [String: Any] let dictionary: [String: Any] = ["key": "value"] let mapYAML: String = try Yams.dump(object: dictionary) ``` ### Response Example (Decoding Array) ```swift // [Any] let array: [Int] = [1, 2, 3] let sequenceYAML: String = try Yams.dump(object: array) let loadedArray: [Int]? = try Yams.load(yaml: sequenceYAML) as? [Int] ``` ``` -------------------------------- ### Add Yams to Bazel WORKSPACE Source: https://github.com/jpsim/yams/blob/main/README.md Include Yams in your Bazel project by adding this http_archive rule to your WORKSPACE file. ```bazel YAMS_GIT_SHA = "SOME_SHA" http_archive( name = "com_github_jpsim_yams", urls = [ "https://github.com/jpsim/Yams/archive/%s.zip" % YAMS_GIT_SHA, ], strip_prefix = "Yams-%s" % YAMS_GIT_SHA, ) ``` -------------------------------- ### Encode and Decode Codable Types with Yams Source: https://github.com/jpsim/yams/blob/main/README.md Demonstrates encoding a Swift struct to YAML and decoding YAML back into a struct using YAMLEncoder and YAMLDecoder. ```swift import Foundation import Yams struct S: Codable { var p: String } let s = S(p: "test") let encoder = YAMLEncoder() let encodedYAML = try encoder.encode(s) encodedYAML == """ p: test """ let decoder = YAMLDecoder() let decoded = try decoder.decode(S.self, from: encodedYAML) s.p == decoded.p ``` -------------------------------- ### Codable Encoding and Decoding Source: https://github.com/jpsim/yams/blob/main/README.md Encode Swift Codable types to YAML strings and decode YAML strings back into Codable types using YAMLEncoder and YAMLDecoder. ```APIDOC ## Codable types ### Description Provides methods for encoding Swift `Encodable` types into YAML strings and decoding YAML strings into Swift `Decodable` types. This strategy offers the lowest computational overhead, equivalent to `Yams.Node`. ### Encoding - **Method:** `YAMLEncoder.encode(_:) - **Description:** Produces a YAML `String` from an instance of a type conforming to `Encodable`. ### Decoding - **Method:** `YAMLDecoder.decode(_:from:) - **Description:** Decodes an instance of a type conforming to `Decodable` from a YAML `String` or `Data`. ### Request Example (Encoding) ```swift import Foundation import Yams struct S: Codable { var p: String } let s = S(p: "test") let encoder = YAMLEncoder() let encodedYAML = try encoder.encode(s) ``` ### Response Example (Decoding) ```swift import Foundation import Yams let decoder = YAMLDecoder() let decoded = try decoder.decode(S.self, from: encodedYAML) // decoded will be an instance of S ``` ``` -------------------------------- ### Consume YAML Array in Swift Source: https://github.com/jpsim/yams/blob/main/Docs.md Parses a YAML string representing an array of strings into a Swift array. Ensure proper error handling for the load operation. ```swift import Yams let yamlString = """ - a - b - c """ dofor { let yamlNode = try Yams.load(yaml: yamlString) if let yamlArray = yamlNode as? [String] { print(yamlArray) } } catch { print("handle error: \(error)") } // Prints: // ["a", "b", "c"] ``` -------------------------------- ### Add Yams to Swift Package Manager Dependencies Source: https://github.com/jpsim/yams/blob/main/README.md Add the Yams package to your Package.swift file to include it in your Swift project. ```swift .package(url: "https://github.com/jpsim/Yams.git", from: "6.2.2") ``` -------------------------------- ### Customize YAML Boolean Parsing in Swift Source: https://github.com/jpsim/yams/blob/main/Docs.md Extends the Yams Constructor to only recognize 'true' and 'false' as boolean literals, ignoring other YAML-compliant boolean representations like 'on' or 'off'. ```swift import Yams extension Constructor { public static func withBoolAsTrueFalse() -> Constructor { var map = defaultScalarMap map[.bool] = Bool.constructUsingOnlyTrueAndFalse return Constructor(map) } } private extension Bool { static func constructUsingOnlyTrueAndFalse(from scalar: Node.Scalar) -> Bool? { switch scalar.string.lowercased() { case "true": return true case "false": return false default: return nil } } } // Usage: let yamlString = """ - true - on - off - false """ if let array = try? Yams.load(yaml: yamlString, .default, .withBoolAsTrueFalse()) as? [Any] { print(array) } // Prints: // [true, "on", "off", false] ``` -------------------------------- ### Yams.Node Encoding and Decoding Source: https://github.com/jpsim/yams/blob/main/README.md Encode and decode using Yams' native Node representation, which provides fine-grained control over YAML structure and format. ```APIDOC ## Yams.Node ### Description Represents Yams' native model for YAML Nodes, offering comprehensive functions for detecting and customizing the YAML format. Computational overhead can be minimized depending on usage. ### Encoding - **Method:** `Yams.serialize(node:) - **Description:** Produces a YAML `String` from an instance of `Node`. ### Decoding - **Method:** `Yams.compose(yaml:) - **Description:** Produces an instance of `Node` from a YAML `String`. ### Request Example (Encoding Node) ```swift var map: Yams.Node = [ "array": [ 1, 2, 3 ] ] map.mapping?.style = .flow map["array"]?.sequence?.style = .flow let yaml = try Yams.serialize(node: map) ``` ### Response Example (Decoding Node) ```swift let node = try Yams.compose(yaml: yaml) // node will be an instance of Yams.Node ``` ``` -------------------------------- ### Integrating Yams with Combine Source: https://github.com/jpsim/yams/blob/main/README.md When Combine is available, YAMLDecoder conforms to TopLevelDecoder, enabling its use with the decode(type:decoder:) operator for network requests. ```swift import Combine import Foundation import Yams func fetchBook(from url: URL) -> AnyPublisher { URLSession.shared.dataTaskPublisher(for: url) .map(\.data) .decode(type: Book.self, decoder: YAMLDecoder()) .eraseToAnyPublisher() } ``` -------------------------------- ### Expand Environment Variables During YAML Parsing in Swift Source: https://github.com/jpsim/yams/blob/main/Docs.md Customizes the Yams Constructor to expand environment variables within YAML strings, such as ${VAR_NAME}. Requires providing an environment dictionary. ```swift import Yams extension Constructor { public static func withEnv(_ env: [String: String]) -> Constructor { var map = defaultScalarMap map[.str] = String.constructExpandingEnvVars(env: env) return Constructor(map) } } private extension String { static func constructExpandingEnvVars(env: [String: String]) -> (_ scalar: Node.Scalar) -> String? { return { (scalar: Node.Scalar) -> String? in return node.string.expandingEnvVars(env: env) } } func expandingEnvVars(env: [String: String]) -> String { var result = self for (key, value) in env { result = result.replacingOccurrences(of: "${\(key)}", with: value) } return result } } // Usage: let yamlString = """ - first - ${SECOND} - SECOND """ let env = ["SECOND": "2"] if let array = try? Yams.load(yaml: yamlString, .default, .withEnv(env)) as? [String] { print(array) } // Prints: // ["first", "2", "SECOND"] ``` -------------------------------- ### Export YAMS Targets Source: https://github.com/jpsim/yams/blob/main/cmake/modules/CMakeLists.txt Exports targets defined in YAMS_EXPORTS to a CMake file. This allows other projects to find and use these targets. ```cmake get_property(YAMS_EXPORTS GLOBAL PROPERTY YAMS_EXPORTS) export(TARGETS ${YAMS_EXPORTS} FILE ${YAMS_EXPORTS_FILE}) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.