-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
455 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
package ortfodb | ||
|
||
const Version = "1.1.0" | ||
const Version = "1.2.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
from typing import List, Optional, Any, Dict, TypeVar, Callable, Type, cast | ||
|
||
|
||
T = TypeVar("T") | ||
|
||
|
||
def from_list(f: Callable[[Any], T], x: Any) -> List[T]: | ||
assert isinstance(x, list) | ||
return [f(y) for y in x] | ||
|
||
|
||
def from_str(x: Any) -> str: | ||
assert isinstance(x, str) | ||
return x | ||
|
||
|
||
def from_none(x: Any) -> Any: | ||
assert x is None | ||
return x | ||
|
||
|
||
def from_union(fs, x): | ||
for f in fs: | ||
try: | ||
return f(x) | ||
except: | ||
pass | ||
assert False | ||
|
||
|
||
def from_dict(f: Callable[[Any], T], x: Any) -> Dict[str, T]: | ||
assert isinstance(x, dict) | ||
return { k: f(v) for (k, v) in x.items() } | ||
|
||
|
||
def from_bool(x: Any) -> bool: | ||
assert isinstance(x, bool) | ||
return x | ||
|
||
|
||
def to_class(c: Type[T], x: Any) -> dict: | ||
assert isinstance(x, c) | ||
return cast(Any, x).to_dict() | ||
|
||
|
||
class ExporterSchema: | ||
log: Optional[List[str]] | ||
"""Log a message. The first argument is the verb, the second is the color, the third is the | ||
message. | ||
""" | ||
run: Optional[str] | ||
"""Run a command in a shell""" | ||
|
||
def __init__(self, log: Optional[List[str]], run: Optional[str]) -> None: | ||
self.log = log | ||
self.run = run | ||
|
||
@staticmethod | ||
def from_dict(obj: Any) -> 'ExporterSchema': | ||
assert isinstance(obj, dict) | ||
log = from_union([lambda x: from_list(from_str, x), from_none], obj.get("log")) | ||
run = from_union([from_str, from_none], obj.get("run")) | ||
return ExporterSchema(log, run) | ||
|
||
def to_dict(self) -> dict: | ||
result: dict = {} | ||
if self.log is not None: | ||
result["log"] = from_union([lambda x: from_list(from_str, x), from_none], self.log) | ||
if self.run is not None: | ||
result["run"] = from_union([from_str, from_none], self.run) | ||
return result | ||
|
||
|
||
class Exporter: | ||
after: Optional[List[ExporterSchema]] | ||
"""Commands to run after the build finishes. Go text template that receives .Data and | ||
.Database, the built database. | ||
""" | ||
before: Optional[List[ExporterSchema]] | ||
"""Commands to run before the build starts. Go text template that receives .Data""" | ||
|
||
data: Optional[Dict[str, Any]] | ||
"""Initial data""" | ||
|
||
description: str | ||
"""Some documentation about the exporter""" | ||
|
||
name: str | ||
"""The name of the exporter""" | ||
|
||
requires: Optional[List[str]] | ||
"""List of programs that are required to be available in the PATH for the exporter to run.""" | ||
|
||
verbose: Optional[bool] | ||
"""If true, will show every command that is run""" | ||
|
||
work: Optional[List[ExporterSchema]] | ||
"""Commands to run during the build, for each work. Go text template that receives .Data and | ||
.Work, the current work. | ||
""" | ||
|
||
def __init__(self, after: Optional[List[ExporterSchema]], before: Optional[List[ExporterSchema]], data: Optional[Dict[str, Any]], description: str, name: str, requires: Optional[List[str]], verbose: Optional[bool], work: Optional[List[ExporterSchema]]) -> None: | ||
self.after = after | ||
self.before = before | ||
self.data = data | ||
self.description = description | ||
self.name = name | ||
self.requires = requires | ||
self.verbose = verbose | ||
self.work = work | ||
|
||
@staticmethod | ||
def from_dict(obj: Any) -> 'Exporter': | ||
assert isinstance(obj, dict) | ||
after = from_union([lambda x: from_list(ExporterSchema.from_dict, x), from_none], obj.get("after")) | ||
before = from_union([lambda x: from_list(ExporterSchema.from_dict, x), from_none], obj.get("before")) | ||
data = from_union([lambda x: from_dict(lambda x: x, x), from_none], obj.get("data")) | ||
description = from_str(obj.get("description")) | ||
name = from_str(obj.get("name")) | ||
requires = from_union([lambda x: from_list(from_str, x), from_none], obj.get("requires")) | ||
verbose = from_union([from_bool, from_none], obj.get("verbose")) | ||
work = from_union([lambda x: from_list(ExporterSchema.from_dict, x), from_none], obj.get("work")) | ||
return Exporter(after, before, data, description, name, requires, verbose, work) | ||
|
||
def to_dict(self) -> dict: | ||
result: dict = {} | ||
if self.after is not None: | ||
result["after"] = from_union([lambda x: from_list(lambda x: to_class(ExporterSchema, x), x), from_none], self.after) | ||
if self.before is not None: | ||
result["before"] = from_union([lambda x: from_list(lambda x: to_class(ExporterSchema, x), x), from_none], self.before) | ||
if self.data is not None: | ||
result["data"] = from_union([lambda x: from_dict(lambda x: x, x), from_none], self.data) | ||
result["description"] = from_str(self.description) | ||
result["name"] = from_str(self.name) | ||
if self.requires is not None: | ||
result["requires"] = from_union([lambda x: from_list(from_str, x), from_none], self.requires) | ||
if self.verbose is not None: | ||
result["verbose"] = from_union([from_bool, from_none], self.verbose) | ||
if self.work is not None: | ||
result["work"] = from_union([lambda x: from_list(lambda x: to_class(ExporterSchema, x), x), from_none], self.work) | ||
return result | ||
|
||
|
||
def exporter_from_dict(s: Any) -> Exporter: | ||
return Exporter.from_dict(s) | ||
|
||
|
||
def exporter_to_dict(x: Exporter) -> Any: | ||
return to_class(Exporter, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Example code that deserializes and serializes the model. | ||
// extern crate serde; | ||
// #[macro_use] | ||
// extern crate serde_derive; | ||
// extern crate serde_json; | ||
// | ||
// use ortfodb::exporter; | ||
// | ||
// fn main() { | ||
// let json = r#"{"answer": 42}"#; | ||
// let model: exporter = serde_json::from_str(&json).unwrap(); | ||
// } | ||
|
||
use serde::{Serialize, Deserialize}; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct Exporter { | ||
/// Commands to run after the build finishes. Go text template that receives .Data and | ||
/// .Database, the built database. | ||
pub after: Option<Vec<ExporterSchema>>, | ||
|
||
/// Commands to run before the build starts. Go text template that receives .Data | ||
pub before: Option<Vec<ExporterSchema>>, | ||
|
||
/// Initial data | ||
pub data: Option<HashMap<String, Option<serde_json::Value>>>, | ||
|
||
/// Some documentation about the exporter | ||
pub description: String, | ||
|
||
/// The name of the exporter | ||
pub name: String, | ||
|
||
/// List of programs that are required to be available in the PATH for the exporter to run. | ||
pub requires: Option<Vec<String>>, | ||
|
||
/// If true, will show every command that is run | ||
pub verbose: Option<bool>, | ||
|
||
/// Commands to run during the build, for each work. Go text template that receives .Data and | ||
/// .Work, the current work. | ||
pub work: Option<Vec<ExporterSchema>>, | ||
} | ||
|
||
#[derive(Serialize, Deserialize)] | ||
pub struct ExporterSchema { | ||
/// Log a message. The first argument is the verb, the second is the color, the third is the | ||
/// message. | ||
pub log: Option<Vec<String>>, | ||
|
||
/// Run a command in a shell | ||
pub run: Option<String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.