Skip to content

Commit

Permalink
new: Support schema types for serde. (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj authored Nov 30, 2023
1 parent 080c2a3 commit 9249970
Show file tree
Hide file tree
Showing 25 changed files with 260 additions and 28 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Changelog

## 0.12.9

#### 🚀 Updates

- Added serde `flatten` support.
- Added `type_serde_json`, `type_serde_toml`, and `type_serde_yaml` features, that implements
schematic types for serde values.

#### 🐞 Fixes

- Updated json schema unknown/any type to be a union of all types, instead of null.

#### ⚙️ Internal

- Updated dependencies.

## 0.12.8

#### ⚙️ Internal
Expand Down
35 changes: 19 additions & 16 deletions Cargo.lock

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

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ regex = "1.10.2"
relative-path = "1.9.0"
rust_decimal = "1.33.1"
semver = "1.0.20"
serde = { version = "1.0.192", features = ["derive"] }
url = "2.4.1"
serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
serde_yaml = "0.9.27"
toml = "0.8.8"
url = "2.5.0"
version_spec = "0.1.5"
warpgate = "0.5.14"
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,9 @@ The following Cargo features are available:
- `type_relative_path` - Implements schematic for the `relative-path` crate.
- `type_rust_decimal` - Implements schematic for the `rust_decimal` crate.
- `type_semver` - Implements schematic for the `semver` crate.
- `type_serde_json` - Implements schematic for the `serde_json` crate.
- `type_serde_toml` - Implements schematic for the `toml` crate.
- `type_serde_yaml` - Implements schematic for the `serde_yaml` crate.
- `type_url` - Implements schematic for the `url` crate.
- `type_version_spec` - Implements schematic for the `version_spec` crate.
- `type_warpgate` - Implements schematic for the `warpgate` crate.
14 changes: 10 additions & 4 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ thiserror = "1.0.50"
tracing = "0.1.40"

# config
garde = { version = "0.16.2", default-features = false, optional = true, features = [
garde = { version = "0.16.3", default-features = false, optional = true, features = [
"regex",
] }
serde = { workspace = true, optional = true }
Expand All @@ -34,16 +34,16 @@ starbase_styles = { version = "0.1.16", optional = true }
indexmap = { version = "2.1.0", optional = true }

# json
serde_json = { version = "1.0.108", optional = true }
serde_json = { workspace = true, optional = true }

# json schema
schemars = { version = "0.8.16", optional = true, default-features = false }

# toml
toml = { version = "0.8.8", optional = true }
toml = { workspace = true, optional = true }

# yaml
serde_yaml = { version = "0.9.27", optional = true }
serde_yaml = { workspace = true, optional = true }

# url
reqwest = { version = "0.11.22", default-features = false, optional = true, features = [
Expand Down Expand Up @@ -72,6 +72,9 @@ type_regex = ["schematic_types/regex"]
type_relative_path = ["schematic_types/relative_path"]
type_rust_decimal = ["schematic_types/rust_decimal"]
type_semver = ["schematic_types/semver"]
type_serde_json = ["schematic_types/serde_json"]
type_serde_toml = ["schematic_types/serde_toml"]
type_serde_yaml = ["schematic_types/serde_yaml"]
type_url = ["schematic_types/url"]
type_version_spec = ["schematic_types/version_spec"]
type_warpgate = ["schematic_types/warpgate"]
Expand All @@ -92,6 +95,9 @@ schematic = { path = ".", features = [
"type_relative_path",
"type_rust_decimal",
"type_semver",
"type_serde_json",
"type_serde_toml",
"type_serde_yaml",
"type_url",
"type_version_spec",
"type_warpgate",
Expand Down
9 changes: 8 additions & 1 deletion crates/config/src/schema/renderers/json_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,14 @@ impl SchemaRenderer<Schema> for JsonSchemaRenderer {

fn render_unknown(&mut self) -> RenderResult<Schema> {
Ok(Schema::Object(SchemaObject {
instance_type: Some(SingleOrVec::Single(Box::new(InstanceType::Null))),
instance_type: Some(SingleOrVec::Vec(vec![
InstanceType::Boolean,
InstanceType::Object,
InstanceType::Array,
InstanceType::Number,
InstanceType::String,
InstanceType::Integer,
])),
..Default::default()
}))
}
Expand Down
3 changes: 3 additions & 0 deletions crates/config/tests/generator_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ struct GenConfig {
spec_unresolved: version_spec::UnresolvedVersionSpec,
id: warpgate::Id,
locator: Option<warpgate::PluginLocator>,
json_value: serde_json::Value,
toml_value: Option<toml::Value>,
yaml_value: serde_yaml::Value,
}

fn create_generator() -> SchemaGenerator {
Expand Down
2 changes: 2 additions & 0 deletions crates/config/tests/macros_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ pub struct ValueTypes {
s3_value: String,
#[setting(nested, exclude)]
other: OptionalValues,
#[setting(flatten)]
rest: HashMap<String, serde_json::Value>,
}

#[derive(Config)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ expression: "fs::read_to_string(file).unwrap()"
"float32",
"float64",
"id",
"jsonValue",
"locator",
"map",
"nested",
Expand All @@ -25,10 +26,12 @@ expression: "fs::read_to_string(file).unwrap()"
"specUnresolved",
"string",
"time",
"tomlValue",
"url",
"vector",
"version",
"versionReq"
"versionReq",
"yamlValue"
],
"properties": {
"boolean": {
Expand Down Expand Up @@ -62,6 +65,16 @@ expression: "fs::read_to_string(file).unwrap()"
"id": {
"type": "string"
},
"jsonValue": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
"locator": {
"anyOf": [
{
Expand Down Expand Up @@ -112,6 +125,23 @@ expression: "fs::read_to_string(file).unwrap()"
"type": "string",
"format": "time"
},
"tomlValue": {
"anyOf": [
{
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
},
{
"type": "null"
}
]
},
"url": {
"anyOf": [
{
Expand Down Expand Up @@ -141,6 +171,16 @@ expression: "fs::read_to_string(file).unwrap()"
},
"versionReq": {
"type": "string"
},
"yamlValue": {
"type": [
"boolean",
"object",
"array",
"number",
"string",
"integer"
]
}
},
"additionalProperties": false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface GenConfig {
float32: number;
float64: number;
id: string;
jsonValue: unknown;
locator: string | null;
map: Record<string, number>;
nested: AnotherConfig;
Expand All @@ -37,9 +38,11 @@ export interface GenConfig {
specUnresolved: string;
string: string;
time: string;
tomlValue: unknown | null;
url: string | null;
vector: string[];
version: string | null;
versionReq: string;
yamlValue: unknown;
}

Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export interface GenConfig {
float32: number;
float64: number;
id: string;
jsonValue: unknown;
locator: string | null;
map: Record<string, number>;
nested: AnotherConfig;
Expand All @@ -33,9 +34,11 @@ export interface GenConfig {
specUnresolved: string;
string: string;
time: string;
tomlValue: unknown | null;
url: string | null;
vector: string[];
version: string | null;
versionReq: string;
yamlValue: unknown;
}

Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export interface GenConfig {
float32: number;
float64: number;
id: string;
jsonValue: unknown;
locator: string | null;
map: Record<string, number>;
nested: AnotherConfig;
Expand All @@ -37,9 +38,11 @@ export interface GenConfig {
specUnresolved: string;
string: string;
time: string;
tomlValue: unknown | null;
url: string | null;
vector: string[];
version: string | null;
versionReq: string;
yamlValue: unknown;
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface GenConfig {
float32: number;
float64: number;
id: string;
jsonValue: unknown;
locator: string | null;
map: Record<string, number>;
nested: AnotherConfig;
Expand All @@ -31,9 +32,11 @@ export interface GenConfig {
specUnresolved: string;
string: string;
time: string;
tomlValue: unknown | null;
url: string | null;
vector: string[];
version: string | null;
versionReq: string;
yamlValue: unknown;
}

Loading

0 comments on commit 9249970

Please sign in to comment.