Describing data in Swift using DSL in .swift file then the command line application renders it into any format like JSON.
Motivation
In writing JSON or something else like that, we may be exhausted in describing repeatedly. We often think somehow it could be done effectively like a programming language. jsonnet is one of the preprocessors solving that. Grain aims to achieve such a function in Swift language.
Create a swift file, then write data, and render it by using Grain tools.
The advantages of using Swift are:
writing data and data structure like thinking in SwiftUI
type checking by Swift compiler
validating the data
Inspireations of using Grain
Writing OpenAPI Specification and using by rendering
Writing JSON Schema
Writing a complex XcodeGen configuration beyond its built-in expressions
Managing mocking APNs files
and more there are a lot of opportunities of using Grain where are using JSON.
the template file allows writing async/await operation. GrainDescriptor is including Alamofire as built-in. For instance, we could create a serizalized data using networking like fetching data.
import GrainDescriptor
serialize {
Endpoint(methods: [
.init(
method: .get,
summary: "Hello",
description: "Hello Get Method",
operationID: "id",
tags: ["Awesome API"]
)
])
}
// MARK: - Components
public struct Endpoint: GrainView {
public var methods: [Method]
public var body: some GrainView {
GrainObject {
for method in methods {
GrainMember(method.method.rawValue) {
method
}
}
}
}
}
public struct Method: GrainView {
public enum HTTPMethod: String {
case get
case post
case put
case delete
}
public var method: HTTPMethod
public var summary: String
public var description: String
public var operationID: String
public var tags: [String]
public var body: some GrainView {
GrainObject {
GrainMember("operationId") { operationID }
GrainMember("description") { description }
GrainMember("summary") { summary }
GrainMember("tags") { tags }
}
}
}
Grain
A data serialization template language in Swift
Describing data in Swift using DSL in
.swift
file then the command line application renders it into any format like JSON.Motivation
In writing JSON or something else like that, we may be exhausted in describing repeatedly.
We often think somehow it could be done effectively like a programming language.
jsonnet is one of the preprocessors solving that.
Grain aims to achieve such a function in Swift language.
Create a swift file, then write data, and render it by using Grain tools.
The advantages of using Swift are:
Inspireations of using Grain
Naming
serialization -> cereal -> grain
Installation
From mint 🌱
From make
other installation ways will be added in the future
Overview
It writes results into given path using same name.
Schema.swift
->Schema.json
Instructions
A basic case
Creates Data.swift describing data
Renders Data.swift as JSON in Terminal
serialize
function is the way to create outputserialize
function declares what renders into data.It also has implicit parameters to customize how to encode and how to save as files.
output parameter accepts how result should be output.
prints out into stdout
write into file in the same directory of source, or specified output directory (using
--output
option)writing file as well as above but uses different name by given name.
It allows us to define multiple times.
This makes files using given name.
Creating component and composing them to describe data efficiently
In Component.swift
Use async/await
the template file allows writing async/await operation.
GrainDescriptor
is includingAlamofire
as built-in.For instance, we could create a serizalized data using networking like fetching data.
Showcases
OpenAPI Specification