Skip to content

Commit

Permalink
Merge pull request #35 from bemyak/fix-numeric-start
Browse files Browse the repository at this point in the history
Fix when map property starts with a number
  • Loading branch information
Marwes committed Jul 26, 2020
2 parents 58072aa + a0485c5 commit 5f79150
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 12 deletions.
13 changes: 12 additions & 1 deletion schemafy_lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,17 @@ fn replace_invalid_identifier_chars(s: &str) -> String {
s.replace(|c: char| !c.is_alphanumeric() && c != '_', "_")
}

fn replace_numeric_start(s: &str) -> String {
if s.chars().next().map(|c| c.is_numeric()).unwrap_or(false) {
format!("_{}", s)
} else {
s.to_string()
}
}

pub fn str_to_ident(s: &str) -> syn::Ident {
let s = replace_invalid_identifier_chars(s);
let s = replace_numeric_start(&s);
let keywords = [
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn",
"for", "if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref",
Expand Down Expand Up @@ -306,7 +315,9 @@ impl<'r> Expander<'r> {
} else {
s.split('/').last().expect("Component")
};
replace_invalid_identifier_chars(&s.to_pascal_case())
let s = &s.to_pascal_case();
let s = replace_invalid_identifier_chars(&s);
replace_numeric_start(&s)
}

fn schema(&self, schema: &'r Schema) -> Cow<'r, Schema> {
Expand Down
6 changes: 5 additions & 1 deletion schemafy_lib/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@
"allOf": { "$ref": "#/definitions/schemaArray" },
"anyOf": { "$ref": "#/definitions/schemaArray" },
"oneOf": { "$ref": "#/definitions/schemaArray" },
"not": { "$ref": "#" }
"not": { "$ref": "#" },
"0": { "$ref": "#" },
"numericalEnum": {
"enum": [ "0", "1", "2", "3", "4", "5", "6" ]
}
},
"dependencies": {
"exclusiveMaximum": [ "maximum" ],
Expand Down
22 changes: 12 additions & 10 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub type PositiveInteger = i64 ; pub type PositiveIntegerDefault0 = serde_json
{
# [serde(rename = "$ref")] pub _ref : Option < String >, #
[serde(rename = "$schema")] pub _schema : Option < String >, #
[serde(rename = "0")] pub _0 : Option < Box < Schema >>, #
[serde(rename = "additionalItems")] pub additional_items : Option <
serde_json :: Value >, # [serde(rename = "additionalProperties")] pub
additional_properties : Option < serde_json :: Value >, #
Expand All @@ -36,14 +37,15 @@ pub type PositiveInteger = i64 ; pub type PositiveIntegerDefault0 = serde_json
: Option < PositiveIntegerDefault0 >, # [serde(rename = "minProperties")]
pub min_properties : Option < PositiveIntegerDefault0 >, pub minimum :
Option < f64 >, # [serde(rename = "multipleOf")] pub multiple_of : Option
< f64 >, pub not : Option < Box < Schema >>, # [serde(rename = "oneOf")]
pub one_of : Option < SchemaArray >, pub pattern : Option < String >, #
[serde(default)] # [serde(rename = "patternProperties")] pub
pattern_properties : :: std :: collections :: BTreeMap < String, Schema >,
# [serde(default)] pub properties : :: std :: collections :: BTreeMap <
String, Schema >, pub required : Option < StringArray >, pub title :
Option < String >, # [serde(default)] #
[serde(with = "::schemafy_core::one_or_many")] # [serde(rename = "type")]
pub type_ : Vec < SimpleTypes >, # [serde(rename = "uniqueItems")] pub
unique_items : Option < bool >
< f64 >, pub not : Option < Box < Schema >>, #
[serde(rename = "numericalEnum")] pub numerical_enum : Option < serde_json
:: Value >, # [serde(rename = "oneOf")] pub one_of : Option < SchemaArray
>, pub pattern : Option < String >, # [serde(default)] #
[serde(rename = "patternProperties")] pub pattern_properties : :: std ::
collections :: BTreeMap < String, Schema >, # [serde(default)] pub
properties : :: std :: collections :: BTreeMap < String, Schema >, pub
required : Option < StringArray >, pub title : Option < String >, #
[serde(default)] # [serde(with = "::schemafy_core::one_or_many")] #
[serde(rename = "type")] pub type_ : Vec < SimpleTypes >, #
[serde(rename = "uniqueItems")] pub unique_items : Option < bool >
}

0 comments on commit 5f79150

Please sign in to comment.