Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ Are you getting weird deserialization issues? Did our procedural macro throw a c
- Typescript
- Go**
- Python** (see list of limitations [here](https://github.com/1Password/typeshare/issues/217))
- ReScript


If there is a language that you want Typeshare to generate definitions for, you can either:
Expand Down
4 changes: 3 additions & 1 deletion cli/data/tests/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,6 @@ package = ''

[kotlin.type_mappings]

[scala.type_mappings]
[scala.type_mappings]

[rescript]
2 changes: 2 additions & 0 deletions cli/data/tests/rescript_header_config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[rescript]
header = "/* Custom ReScript header */"
2 changes: 2 additions & 0 deletions cli/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ pub enum AvailableLanguage {
Go,
#[cfg(feature = "python")]
Python,
#[value(name = "rescript")]
ReScript,
}

#[derive(clap::Parser)]
Expand Down
9 changes: 9 additions & 0 deletions cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ pub struct GoParams {
pub type_mappings: HashMap<String, String>,
}

#[derive(Default, Serialize, Deserialize, PartialEq, Eq, Debug)]
#[serde(default)]
pub struct ReScriptParams {
pub type_mappings: HashMap<String, String>,
pub default_decorators: Vec<String>,
pub header: Option<String>,
}

/// The parameters that are used to configure the behaviour of typeshare
/// from the configuration file `typeshare.toml`
#[derive(Serialize, Deserialize, Default, Debug, PartialEq)]
Expand All @@ -75,6 +83,7 @@ pub(crate) struct Config {
pub python: PythonParams,
#[cfg(feature = "go")]
pub go: GoParams,
pub rescript: ReScriptParams,
#[serde(skip)]
pub target_os: Vec<String>,
}
Expand Down
11 changes: 10 additions & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ use typeshare_core::language::Go;
use typeshare_core::language::Python;
use typeshare_core::{
context::ParseContext,
language::{CrateName, Kotlin, Language, Scala, SupportedLanguage, Swift, TypeScript},
language::{
CrateName, Kotlin, Language, ReScript, Scala, SupportedLanguage, Swift, TypeScript,
},
parser::ParsedData,
reconcile::reconcile_aliases,
};
Expand Down Expand Up @@ -95,6 +97,7 @@ fn generate_types(config_file: Option<&Path>, options: &Args) -> anyhow::Result<
args::AvailableLanguage::Go => SupportedLanguage::Go,
#[cfg(feature = "python")]
args::AvailableLanguage::Python => SupportedLanguage::Python,
args::AvailableLanguage::ReScript => SupportedLanguage::ReScript,
},
};

Expand Down Expand Up @@ -236,6 +239,12 @@ fn language(
SupportedLanguage::Python => {
panic!("python support is currently experimental and must be enabled as a feature flag for typeshare-cli")
}
SupportedLanguage::ReScript => Box::new(ReScript {
type_mappings: config.rescript.type_mappings,
default_decorators: config.rescript.default_decorators,
header: config.rescript.header,
..Default::default()
}),
}
}

Expand Down
1 change: 1 addition & 0 deletions cli/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ fn output_file_name(language_type: SupportedLanguage, crate_name: &CrateName) ->
SupportedLanguage::Swift => pascal_case(),
SupportedLanguage::TypeScript => snake_case(),
SupportedLanguage::Python => snake_case(),
SupportedLanguage::ReScript => pascal_case(),
}
}

Expand Down
24 changes: 24 additions & 0 deletions core/data/tests/can_generate_algebraic_enum/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** Struct comment */
type itemDetailsFieldValue = {
}

/** Enum comment */
@tag("type")
type advancedColors =
/** This is a case comment */
| @as("String") String({ content: string })
| @as("Number") Number({ content: int })
| @as("UnsignedNumber") UnsignedNumber({ content: int })
| @as("NumberArray") NumberArray({ content: array<int> })
/** Comment on the last element */
| @as("ReallyCoolType") ReallyCoolType({ content: itemDetailsFieldValue })

@tag("type")
type advancedColors2 =
/** This is a case comment */
| @as("string") String({ content: string })
| @as("number") Number({ content: int })
| @as("number-array") NumberArray({ content: array<int> })
/** Comment on the last element */
| @as("really-cool-type") ReallyCoolType({ content: itemDetailsFieldValue })

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@tag("type")
type SomeEnum =
| @as("A") A
| @as("C") C(int)

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/** Enum keeping track of who autofilled a field */
/** This field was autofilled by us */
type autofilledByUs = {
/** The UUID for the fill */
uuid: string,
}

/** Something else autofilled this field */
type autofilledBySomethingElse = {
/** The UUID for the fill */
uuid: string,
}

@tag("type")
type autofilledBy =
/** This field was autofilled by us */
| @as("Us") Us({ content: autofilledByUs })
/** Something else autofilled this field */
| @as("SomethingElse") SomethingElse({ content: autofilledBySomethingElse })

7 changes: 7 additions & 0 deletions core/data/tests/can_generate_bare_string_enum/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/** This is a comment. */
type colors =
| @as("Red") Red
| @as("Blue") Blue
| @as("Green") Green


4 changes: 4 additions & 0 deletions core/data/tests/can_generate_double_option_pattern/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type someStruct = {
field_a?: option<int>,
}

8 changes: 8 additions & 0 deletions core/data/tests/can_generate_empty_algebraic_enum/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type addressDetails = {
}

@tag("type")
type address =
| @as("FixedAddress") FixedAddress({ content: addressDetails })
| @as("NoFixedAddress") NoFixedAddress

40 changes: 40 additions & 0 deletions core/data/tests/can_generate_generic_enum/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@tag("type")
type genericEnum<'a, 'b> =
| @as("VariantA") VariantA({ content: 'a })
| @as("VariantB") VariantB({ content: 'b })

type structUsingGenericEnum = {
enum_field: genericEnum<string, int>,
}

@tag("type")
type genericEnumUsingGenericEnum<'t> =
| @as("VariantC") VariantC({ content: genericEnum<'t, 't> })
| @as("VariantD") VariantD({ content: genericEnum<string, Dict.t<'t>> })
| @as("VariantE") VariantE({ content: genericEnum<string, int> })

type genericEnumsUsingStructVariantsVariantF<'t, 'u> = {
action: 't,
}

type genericEnumsUsingStructVariantsVariantG<'t, 'u> = {
action: 't,
response: 'u,
}

type genericEnumsUsingStructVariantsVariantH<'t, 'u> = {
non_generic: int,
}

type genericEnumsUsingStructVariantsVariantI<'t, 'u> = {
vec: array<'t>,
action: myType<'t, 'u>,
}

@tag("type")
type genericEnumsUsingStructVariants<'t, 'u> =
| @as("VariantF") VariantF({ content: genericEnumsUsingStructVariantsVariantF<'t, 'u> })
| @as("VariantG") VariantG({ content: genericEnumsUsingStructVariantsVariantG<'t, 'u> })
| @as("VariantH") VariantH({ content: genericEnumsUsingStructVariantsVariantH<'t, 'u> })
| @as("VariantI") VariantI({ content: genericEnumsUsingStructVariantsVariantI<'t, 'u> })

18 changes: 18 additions & 0 deletions core/data/tests/can_generate_generic_struct/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
type genericStruct<'a, 'b> = {
field_a: 'a,
field_b: array<'b>,
}

type genericStructUsingGenericStruct<'t> = {
struct_field: genericStruct<string, 't>,
second_struct_field: genericStruct<'t, string>,
third_struct_field: genericStruct<'t, array<'t>>,
}

@tag("type")
type enumUsingGenericStruct =
| @as("VariantA") VariantA({ content: genericStruct<string, float> })
| @as("VariantB") VariantB({ content: genericStruct<string, int> })
| @as("VariantC") VariantC({ content: genericStruct<string, bool> })
| @as("VariantD") VariantD({ content: genericStructUsingGenericStruct<unit> })

4 changes: 4 additions & 0 deletions core/data/tests/can_generate_generic_type_alias/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type genericTypeAlias<'t> = array<'t>

type nonGenericAlias = genericTypeAlias<string>

11 changes: 11 additions & 0 deletions core/data/tests/can_generate_simple_enum/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* This is a comment.
* Continued lovingly here
*/
type colors =
| @as("Red") Red
| @as("Blue") Blue
/** Green is a cool color */
| @as("Green") Green


Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type location = {
}

/** This is a comment. */
type person = {
/** This is another comment */
name: string,
age: int,
info?: string,
emails: array<string>,
location: location,
}

4 changes: 4 additions & 0 deletions core/data/tests/can_generate_slice_of_user_type/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type video = {
tags: array<tag>,
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type myStruct = {
a: int,
c: int,
}

41 changes: 41 additions & 0 deletions core/data/tests/can_handle_anonymous_struct/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/** Enum keeping track of who autofilled a field */
/** This field was autofilled by us */
type autofilledByUs = {
/** The UUID for the fill */
uuid: string,
}

/** Something else autofilled this field */
type autofilledBySomethingElse = {
/** The UUID for the fill */
uuid: string,
/** Some other thing */
thing: int,
}

@tag("type")
type autofilledBy =
/** This field was autofilled by us */
| @as("Us") Us({ content: autofilledByUs })
/** Something else autofilled this field */
| @as("SomethingElse") SomethingElse({ content: autofilledBySomethingElse })

/** This is a comment (yareek sameek wuz here) */
type enumWithManyVariantsAnonVariant = {
uuid: string,
}

type enumWithManyVariantsAnotherAnonVariant = {
uuid: string,
thing: int,
}

@tag("type")
type enumWithManyVariants =
| @as("UnitVariant") UnitVariant
| @as("TupleVariantString") TupleVariantString({ content: string })
| @as("AnonVariant") AnonVariant({ content: enumWithManyVariantsAnonVariant })
| @as("TupleVariantInt") TupleVariantInt({ content: int })
| @as("AnotherUnitVariant") AnotherUnitVariant
| @as("AnotherAnonVariant") AnotherAnonVariant({ content: enumWithManyVariantsAnotherAnonVariant })

5 changes: 5 additions & 0 deletions core/data/tests/can_handle_quote_in_serde_rename/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** This is a comment. */
type colors =
| @as("Green\"") Green


13 changes: 13 additions & 0 deletions core/data/tests/can_handle_serde_rename/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type otherType = {
}

/** This is a comment. */
type person = {
name: string,
age: int,
extraSpecialFieldOne: int,
extraSpecialFieldTwo?: array<string>,
nonStandardDataType: otherType,
nonStandardDataTypeInArray?: array<otherType>,
}

16 changes: 16 additions & 0 deletions core/data/tests/can_handle_serde_rename_all/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** This is a Person struct with camelCase rename */
type person = {
firstName: string,
lastName: string,
age: int,
extraSpecialField1: int,
extraSpecialField2?: array<string>,
}

/** This is a Person2 struct with UPPERCASE rename */
type person2 = {
FIRST_NAME: string,
LAST_NAME: string,
AGE: int,
}

13 changes: 13 additions & 0 deletions core/data/tests/can_handle_serde_rename_on_top_level/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type otherType = {
}

/** This is a comment. */
type personTwo = {
name: string,
age: int,
extraSpecialFieldOne: int,
extraSpecialFieldTwo?: array<string>,
nonStandardDataType: otherType,
nonStandardDataTypeInArray?: array<otherType>,
}

10 changes: 10 additions & 0 deletions core/data/tests/can_handle_unit_type/output.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** This struct has a unit field */
type structHasVoidType = {
thisIsAUnit: unit,
}

/** This enum has a variant associated with unit data */
@tag("type")
type enumHasVoidType =
| @as("hasAUnit") HasAUnit({ content: unit })

Loading