Skip to content

Commit 82bb8a0

Browse files
authored
Improve support for empty and non-ASCII-alphabetic properties, enum values, and definitions (#17)
* TypeScript: Support invalid identifiers as properties * Try to fix bad name generation * Add support for empty and nonascii definitions/enums/properties
1 parent f05e633 commit 82bb8a0

File tree

54 files changed

+1899
-13
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+1899
-13
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/core/src/target/inflect.rs

+23-9
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,21 @@ impl Inflector for TailInflector {
6666
fn decompose(s: &str) -> Vec<String> {
6767
let mut out: Vec<Vec<char>> = vec![vec![]];
6868
for c in s.chars() {
69-
if c.is_whitespace() || c == '-' || c == '_' || c == ':' {
69+
// Non-ASCII alphanumeric characters, such as whitespace, dashes,
70+
// underscores, or non-ASCII characters, are presumed to always be
71+
// delimiters.
72+
if !c.is_ascii_alphanumeric() {
7073
out.push(vec![]);
7174
continue;
7275
}
7376

77+
// Do not allow a part to start with a digit. Most languages prohibit
78+
// digits at the beginning of identifiers. Just ignore the digit to make
79+
// this happen.
80+
if c.is_ascii_digit() && out.last().unwrap().is_empty() {
81+
continue;
82+
}
83+
7484
if let Some(last_char) = out.last().unwrap().last() {
7585
if last_char.is_lowercase() && c.is_uppercase() {
7686
out.push(vec![]);
@@ -154,13 +164,17 @@ impl Case {
154164
}
155165

156166
pub fn inflect(&self, words: &[String]) -> String {
157-
if words.is_empty() {
158-
return "".to_owned();
167+
let mut word_parts: Vec<_> = words.into_iter().flat_map(|word| decompose(word)).collect();
168+
169+
// If after decomposing the word into its parts (and after the
170+
// associated stripping of non-ASCII alphanumerics) we don't have any
171+
// words to work with, then inflect a "default name" instead.
172+
if word_parts.is_empty() {
173+
word_parts = vec!["default".into(), "name".into()];
159174
}
160175

161-
let parts: Vec<_> = words
176+
let parts: Vec<_> = word_parts
162177
.into_iter()
163-
.flat_map(|word| decompose(word))
164178
.enumerate()
165179
.map(|(i, word)| {
166180
if self.initialisms.contains(&word) {
@@ -245,7 +259,7 @@ mod tests {
245259

246260
#[test]
247261
fn test_camel_case() {
248-
assert_eq!("", Case::camel_case().inflect(&[]));
262+
assert_eq!("defaultName", Case::camel_case().inflect(&[]));
249263

250264
assert_eq!("foo", Case::camel_case().inflect(&["foo".to_owned()]));
251265
assert_eq!(
@@ -260,7 +274,7 @@ mod tests {
260274

261275
#[test]
262276
fn test_pascal_case() {
263-
assert_eq!("", Case::pascal_case().inflect(&[]));
277+
assert_eq!("DefaultName", Case::pascal_case().inflect(&[]));
264278

265279
assert_eq!("Foo", Case::pascal_case().inflect(&["foo".to_owned()]));
266280
assert_eq!(
@@ -275,7 +289,7 @@ mod tests {
275289

276290
#[test]
277291
fn test_snake_case() {
278-
assert_eq!("", Case::snake_case().inflect(&[]));
292+
assert_eq!("default_name", Case::snake_case().inflect(&[]));
279293

280294
assert_eq!("foo", Case::snake_case().inflect(&["foo".to_owned()]));
281295
assert_eq!(
@@ -290,7 +304,7 @@ mod tests {
290304

291305
#[test]
292306
fn test_screaming_snake_case() {
293-
assert_eq!("", Case::screaming_snake_case().inflect(&[]));
307+
assert_eq!("DEFAULT_NAME", Case::screaming_snake_case().inflect(&[]));
294308

295309
assert_eq!(
296310
"FOO",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(DefaultNameJsonConverter))]
10+
public class DefaultName
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class DefaultNameJsonConverter : JsonConverter<DefaultName>
19+
{
20+
public override DefaultName Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new DefaultName { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, DefaultName value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(FooJsonConverter))]
10+
public class Foo
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class FooJsonConverter : JsonConverter<Foo>
19+
{
20+
public override Foo Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new Foo { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, Foo value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(Foo0JsonConverter))]
10+
public class Foo0
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class Foo0JsonConverter : JsonConverter<Foo0>
19+
{
20+
public override Foo0 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new Foo0 { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, Foo0 value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(Foo0barJsonConverter))]
10+
public class Foo0bar
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class Foo0barJsonConverter : JsonConverter<Foo0bar>
19+
{
20+
public override Foo0bar Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new Foo0bar { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, Foo0bar value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(Foo1JsonConverter))]
10+
public class Foo1
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class Foo1JsonConverter : JsonConverter<Foo1>
19+
{
20+
public override Foo1 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new Foo1 { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, Foo1 value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(FooBarJsonConverter))]
10+
public class FooBar
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class FooBarJsonConverter : JsonConverter<FooBar>
19+
{
20+
public override FooBar Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new FooBar { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, FooBar value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(FooBar0JsonConverter))]
10+
public class FooBar0
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class FooBar0JsonConverter : JsonConverter<FooBar0>
19+
{
20+
public override FooBar0 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new FooBar0 { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, FooBar0 value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(FooBar1JsonConverter))]
10+
public class FooBar1
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class FooBar1JsonConverter : JsonConverter<FooBar1>
19+
{
20+
public override FooBar1 Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new FooBar1 { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, FooBar1 value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Code generated by jtd-codegen for C# + System.Text.Json v0.2.0
2+
3+
using System;
4+
using System.Text.Json;
5+
using System.Text.Json.Serialization;
6+
7+
namespace JtdCodegenE2E
8+
{
9+
[JsonConverter(typeof(RootJsonConverter))]
10+
public class Root
11+
{
12+
/// <summary>
13+
/// The underlying data being wrapped.
14+
/// </summary>
15+
public string Value { get; set; }
16+
}
17+
18+
public class RootJsonConverter : JsonConverter<Root>
19+
{
20+
public override Root Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
21+
{
22+
return new Root { Value = JsonSerializer.Deserialize<string>(ref reader, options) };
23+
}
24+
25+
public override void Write(Utf8JsonWriter writer, Root value, JsonSerializerOptions options)
26+
{
27+
JsonSerializer.Serialize<string>(writer, value.Value, options);
28+
}
29+
}
30+
}

0 commit comments

Comments
 (0)