Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: codegen succeeds for error type args that reference objects #264

Merged
merged 10 commits into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
5 changes: 5 additions & 0 deletions changelog/@unreleased/pr-264.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: fix
fix:
description: Codegen succeeds for error type args that reference objects
links:
- https://github.com/palantir/conjure-rust/pull/264
12 changes: 12 additions & 0 deletions conjure-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,18 @@ impl Context {
);
}

for def in defs.errors() {
context.types.insert(
def.error_name().clone(),
TypeContext {
def: TypeDefinition::Object(def.object_definition()),
has_double: Cell::new(None),
is_copy: Cell::new(None),
log_safety: RefCell::new(CachedLogSafety::Uncomputed),
},
);
}

context
}

Expand Down
16 changes: 11 additions & 5 deletions conjure-codegen/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ use crate::context::Context;
use crate::objects;
use crate::types::{ErrorDefinition, ObjectDefinition};

impl ErrorDefinition {
pub fn object_definition(&self) -> ObjectDefinition {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we instead make this a free function? I try to avoid adding new impls on generated types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, though lemme know if you have a preferred naming convention

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

ObjectDefinition::builder()
.type_name(self.error_name().clone())
.fields(self.safe_args().iter().chain(self.unsafe_args()).cloned())
.docs(self.docs().cloned())
.build()
}
}

pub fn generate(ctx: &Context, def: &ErrorDefinition) -> TokenStream {
let object = ObjectDefinition::builder()
.type_name(def.error_name().clone())
.fields(def.safe_args().iter().chain(def.unsafe_args()).cloned())
.docs(def.docs().cloned())
.build();
let object = def.object_definition();
let object_def = objects::generate(ctx, &object);
let error_type = generate_error_type(ctx, def);

Expand Down
9 changes: 7 additions & 2 deletions conjure-test/src/test/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,16 @@ use crate::types::*;

#[test]
fn error_serialization() {
let error = SimpleError::new("hello", 15, false);
let error = SimpleError::builder()
.foo("hello")
.bar(15)
.baz(EmptyObject::new())
.unsafe_foo(false)
.build();

assert_eq!(error.code(), ErrorCode::Internal);
assert_eq!(error.name(), "Test:SimpleError");
assert_eq!(error.safe_args(), &["bar", "foo"]);
assert_eq!(error.safe_args(), &["bar", "baz", "foo"]);

let encoded = conjure_error::encode(&error);

Expand Down
9 changes: 9 additions & 0 deletions conjure-test/test-ir.json

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

1 change: 1 addition & 0 deletions conjure-test/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ types:
safe-args:
foo: string
bar: integer
baz: EmptyObject
unsafe-args:
unsafeFoo: boolean

Expand Down