Skip to content

Commit fcab694

Browse files
[typeshare] Support external tags for enums (1Password#7)
* For complex enums the default serde serialization is externally tagged, to avoid having to re-write our rust logic to use internal tags via `#[serde(tag = "type", content = "content")]` we are going to add manual support for it in `typescript` * Bumping version to `1.12.1` * Adding a `--dry-run` flag to allow building locally ``` enum MyEnum { blahblah { name: String } blahblah2 { age: u64 } } ``` ``` export type MyEnum = { "type": "blahblah", content: { "name": string }} | { "type": "blahblah2", content: { "age": number }} ``` ``` export type MyEnum = { "blahblah": { "name": string }} | { "blahblah2": { "age": number }} ``` * CI passes * Revert
1 parent 97e1a89 commit fcab694

File tree

4 files changed

+73
-44
lines changed

4 files changed

+73
-44
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ dist/
1717
uploads.txt
1818
dist-manifest.json
1919
.intentionally-empty-file.o
20+
.DS_Store

core/src/language/typescript.rs

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -291,36 +291,63 @@ impl TypeScript {
291291
writeln!(w)?;
292292
self.write_comments(w, 1, &v.shared().comments)?;
293293
match v {
294-
RustEnumVariant::Unit(shared) => write!(
295-
w,
296-
"\t| {{ {}: {:?}, {}?: undefined }}",
297-
tag_key, shared.id.renamed, content_key
298-
),
294+
RustEnumVariant::Unit(shared) => {
295+
if !tag_key.is_empty() {
296+
if !content_key.is_empty() {
297+
write!(
298+
w,
299+
"\t| {{ {}: {:?}, {}?: undefined }}",
300+
tag_key, shared.id.renamed, content_key
301+
)
302+
} else {
303+
write!(w, "\t| {{ {}: {:?} }}", tag_key, shared.id.renamed)
304+
}
305+
} else {
306+
write!(w, "\t| {:?}", shared.id.renamed)
307+
}
308+
}
299309
RustEnumVariant::Tuple { ty, shared } => {
300310
let r#type = self
301311
.format_type(ty, e.shared().generic_types.as_slice())
302-
.map_err(io::Error::other)?;
303-
write!(
304-
w,
305-
"\t| {{ {}: {:?}, {}{}: {} }}",
306-
tag_key,
307-
shared.id.renamed,
308-
content_key,
309-
if ty.is_optional() {
310-
"?"
312+
.map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
313+
if !tag_key.is_empty() {
314+
if !content_key.is_empty() {
315+
write!(
316+
w,
317+
"\t| {{ {}: {:?}, {}{}: {} }}",
318+
tag_key,
319+
shared.id.renamed,
320+
content_key,
321+
ty.is_optional().then_some("?").unwrap_or_default(),
322+
r#type
323+
)
311324
} else {
312-
Default::default()
313-
},
314-
r#type
315-
)
325+
panic!("Tuple variants must have a content key if they have a tag key: {:?}", shared.id.original)
326+
}
327+
} else {
328+
write!(
329+
w,
330+
"\t| {{ {:?}{}: {} }}",
331+
shared.id.renamed,
332+
ty.is_optional().then_some("?").unwrap_or_default(),
333+
r#type
334+
)
335+
}
316336
}
317337
RustEnumVariant::AnonymousStruct { fields, shared } => {
318-
writeln!(
319-
w,
320-
"\t| {{ {}: {:?}, {}: {{",
321-
tag_key, shared.id.renamed, content_key
322-
)?;
323-
338+
if !tag_key.is_empty() {
339+
if !content_key.is_empty() {
340+
writeln!(
341+
w,
342+
"\t| {{ {}: {:?}, {}: {{",
343+
tag_key, shared.id.renamed, content_key
344+
)?;
345+
} else {
346+
panic!("Struct variants must have a content key if they have a tag key: {:?}", shared.id.original)
347+
}
348+
} else {
349+
writeln!(w, "\t| {{ {:?}: {{", shared.id.renamed)?;
350+
}
324351
fields.iter().try_for_each(|f| {
325352
self.write_field(w, f, e.shared().generic_types.as_slice())
326353
})?;

core/src/parser.rs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -378,18 +378,8 @@ pub(crate) fn parse_enum(
378378
} else {
379379
// At least one enum variant is either a tuple or an anonymous struct
380380

381-
let tag_key = maybe_tag_key.ok_or_else(|| {
382-
ParseError::SerdeTagRequired {
383-
enum_ident: original_enum_ident.clone(),
384-
}
385-
.with_span(e.span())
386-
})?;
387-
let content_key = maybe_content_key.ok_or_else(|| {
388-
ParseError::SerdeContentRequired {
389-
enum_ident: original_enum_ident.clone(),
390-
}
391-
.with_span(e.span())
392-
})?;
381+
let tag_key = maybe_tag_key.unwrap_or_default();
382+
let content_key = maybe_content_key.unwrap_or_default();
393383

394384
Ok(RustItem::Enum(RustEnum::Algebraic {
395385
tag_key,

scripts/build.sh

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ function help() {
1414
echo " * \"aarch64-unknown-linux-gnu\""
1515
echo " * \"x86_64-unknown-linux-gnu\""
1616
echo " * \"x86_64-pc-windows-msvc\""
17+
echo " --dry-run Run the script without releasing"
1718
exit 0
1819
}
1920

@@ -26,6 +27,8 @@ function help() {
2627
TARGET=""
2728
# --version <version>
2829
VERSION=""
30+
# --dry-run
31+
DRY_RUN=false
2932

3033
# Parse command line arguments
3134
while [[ $# -gt 0 ]]; do
@@ -41,6 +44,10 @@ while [[ $# -gt 0 ]]; do
4144
shift
4245
shift
4346
;;
47+
--dry-run)
48+
DRY_RUN=true
49+
shift
50+
;;
4451
-h|--help)
4552
help
4653
;;
@@ -52,12 +59,12 @@ while [[ $# -gt 0 ]]; do
5259
done
5360

5461
# Check if required arguments are provided
55-
if [ -z "${TARGET}" ]; then
62+
if [[ -z "${TARGET}" ]]; then
5663
echo "Missing required argument: --target"
5764
exit 1
5865
fi
5966

60-
if [ -z "$VERSION" ]; then
67+
if [[ -z "$VERSION" ]]; then
6168
echo "Missing required argument: --version"
6269
exit 1
6370
fi
@@ -87,9 +94,13 @@ echo "{\"artifacts\": [{\"path\": \"dist/${ZIP_NAME}\"}]}" > dist-manifest.json
8794
echo "Build complete, contents of dist-manifest.json:"
8895
cat dist-manifest.json
8996

90-
# Upload to release
91-
cat dist-manifest.json | jq --raw-output ".artifacts[]?.path | select( . != null )" > uploads.txt
92-
echo "uploading..."
93-
cat uploads.txt
94-
gh release upload ${VERSION} $(cat uploads.txt)
95-
echo "uploaded!"
97+
if [[ "$DRY_RUN" == true ]]; then
98+
echo "ℹ️ Dry run, skipping release upload"
99+
else
100+
# Upload to release
101+
cat dist-manifest.json | jq --raw-output ".artifacts[]?.path | select( . != null )" > uploads.txt
102+
echo "uploading..."
103+
cat uploads.txt
104+
gh release upload ${VERSION} $(cat uploads.txt)
105+
echo "uploaded!"
106+
fi

0 commit comments

Comments
 (0)