Skip to content

Commit

Permalink
User-info, yaml support (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
muukii authored Oct 23, 2022
1 parent b5b570d commit 20bb4c3
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 9 deletions.
4 changes: 4 additions & 0 deletions .swiftpm/xcode/xcshareddata/xcschemes/grain.xcscheme
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@
<CommandLineArguments>
<CommandLineArgument
argument = "./Sources/Fixture.swift ./fixtures/openapi.swift --output /Users/muukii/Desktop"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "./fixtures/context.swift --user-info &apos;{&quot;name&quot; : &quot;muukii&quot;}&apos;"
isEnabled = "YES">
</CommandLineArgument>
</CommandLineArguments>
Expand Down
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@
"revision" : "0b77e67c484e532444ceeab60119b8536f8cd648",
"version" : "0.3.0"
}
},
{
"identity" : "yams",
"kind" : "remoteSourceControl",
"location" : "https://github.com/jpsim/Yams.git",
"state" : {
"revision" : "01835dc202670b5bb90d07f3eae41867e9ed29f6",
"version" : "5.0.1"
}
}
],
"version" : 2
Expand Down
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@ let package = Package(
dependencies: [
.package(url: "https://github.com/apple/swift-argument-parser", from: "1.1.0"),
.package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.2.7"),
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.6.2")
.package(url: "https://github.com/Alamofire/Alamofire.git", from: "5.6.2"),
.package(url: "https://github.com/jpsim/Yams.git", from: "5.0.1")
],
targets: [
.target(
name: "GrainDescriptor",
dependencies: [
"Yams",
"Alamofire",
.product(name: "SwiftToolsSupport-auto", package: "swift-tools-support-core"),
],
Expand Down
10 changes: 5 additions & 5 deletions Sources/Fixture.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import GrainDescriptor
import Foundation
import Alamofire

let response = try await AF.request("https://httpbin.org/get").serializingString().value
//let response = try await AF.request("https://httpbin.org/get").serializingString().value

serialize {
serialize(.yaml) {

GrainObject {
GrainMember("data") {
Expand All @@ -13,9 +13,9 @@ serialize {
.init(name: "B", age: 2),
])
}
GrainMember("result") {
response
}
// GrainMember("result") {
// response
// }
GrainMember("context") {
context.filePath.pathString
}
Expand Down
6 changes: 4 additions & 2 deletions Sources/Grain/CLI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,9 @@ struct CLI: AsyncParsableCommand {

}

@Argument var targetFilePaths: [String]
@Argument(completion: .directory) var targetFilePaths: [String]
@Option(name: .customLong("output")) var outputDirectory: String?
@Option(help: "a parameter to inject json as Context") var userInfo: String?
@Flag var verbose = false

func run() async throws {
Expand All @@ -59,7 +60,8 @@ struct CLI: AsyncParsableCommand {

let context = GrainDescriptor.Context(
filePath: path,
outputDir: outputDirectory.map { AbsolutePath($0, relativeTo: localFileSystem.currentWorkingDirectory!) }
outputDir: outputDirectory.map { AbsolutePath($0, relativeTo: localFileSystem.currentWorkingDirectory!) },
userInfoString: userInfo
)

try await self.render(path, context: context)
Expand Down
69 changes: 68 additions & 1 deletion Sources/GrainDescriptor/Entrypoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ import Alamofire
@_implementationOnly import Darwin.C
import Foundation
import TSCBasic
import Yams

/**
The strategy to use serialization as `Data`
*/
public struct Serialization {

private let _encode: (any GrainView) throws -> Data
Expand Down Expand Up @@ -43,6 +47,45 @@ public struct Serialization {
)

}

public static var plist: Self {
plist(outputFormat: .xml)
}

public static func plist(
outputFormat: PropertyListSerialization.PropertyListFormat
) -> Self {

let encoder = PropertyListEncoder()

return .init(
fileExtension: "plist",
encode: {
try encoder.encode($0)
}
)

}

public static var yaml: Self {
yaml(options: .init())
}

public static func yaml(options: YAMLEncoder.Options) -> Self {

let encoder = YAMLEncoder()

encoder.options = options

return .init(
fileExtension: "yml",
encode: {
let string = try encoder.encode($0)
return string.data(using: .utf8)!
}
)

}

public func encode(_ view: some GrainView) throws -> Data {
return try _encode(view)
Expand All @@ -51,16 +94,23 @@ public struct Serialization {
}

public struct Context: Codable {

public enum DomainError: Int, Error {
case contextNotProvided
}

public let filePath: AbsolutePath
public let outputDir: AbsolutePath?
public let userInfoString: String?

public init(
filePath: AbsolutePath,
outputDir: AbsolutePath?
outputDir: AbsolutePath?,
userInfoString: String?
) {
self.filePath = filePath
self.outputDir = outputDir
self.userInfoString = userInfoString
}

public func json() -> String {
Expand All @@ -79,6 +129,15 @@ public struct Context: Codable {
let url = URL(fileURLWithPath: path.pathString)
try data.write(to: url, options: [.atomic])
}

public func userInfo<T: Decodable>(_ decodableType: T.Type) throws -> T {
guard let userInfoString else {
throw DomainError.contextNotProvided
}
let decoder = JSONDecoder()
let decoded = try decoder.decode(decodableType.self, from: userInfoString.data(using: .utf8)!)
return decoded
}

}

Expand Down Expand Up @@ -144,6 +203,14 @@ public struct Output {

}

public func serialize(
_ serialization: Serialization = .json,
output: Output = .stdout,
@GrainBuilder _ thunk: () throws -> some GrainView
) {
serialize(serialization: serialization, output: output, thunk)
}

public func serialize(
serialization: Serialization = .json,
output: Output = .stdout,
Expand Down
19 changes: 19 additions & 0 deletions fixtures/context.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import GrainDescriptor

// ./fixtures/context.swift --user-info '{"name" : "muukii"}'

struct Parameter: Decodable {
var name: String
}

let parameter = try context.userInfo(Parameter.self)

serialize(.json) {

GrainObject {
GrainMember("context") {
parameter.name
}
}

}

0 comments on commit 20bb4c3

Please sign in to comment.