Skip to content

Commit

Permalink
Server generation support
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed May 4, 2019
1 parent 9a25005 commit 971f07e
Show file tree
Hide file tree
Showing 48 changed files with 3,258 additions and 186 deletions.
110 changes: 59 additions & 51 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions conjure-codegen/src/aliases.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ pub fn generate(ctx: &Context, def: &AliasDefinition) -> TokenStream {
conjure_object::Plain::fmt(&self.0, fmt)
}
}

impl conjure_object::FromPlain for #name {
type Err = <#alias as conjure_object::FromPlain>::Err;

#[inline]
fn from_plain(s: &str) -> #result<#name, Self::Err> {
conjure_object::FromPlain::from_plain(s).map(#name)
}
}
}
} else {
quote!()
Expand Down
2 changes: 1 addition & 1 deletion conjure-codegen/src/clients.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ fn setup_headers(
// match on an aliased option so we'll just iterate.
if ctx.is_iterable(argument.type_()) {
parameter = quote! {
for #name in #name {
for #name in #name.iter() {
#parameter
}
}
Expand Down
46 changes: 46 additions & 0 deletions conjure-codegen/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -720,6 +720,42 @@ impl Context {
}
}

pub fn is_list(&self, def: &Type) -> bool {
match def {
Type::List(_) => true,
Type::Primitive(_) | Type::Optional(_) | Type::Set(_) | Type::Map(_) => false,
Type::Reference(def) => self.is_list_ref(def),
Type::External(def) => self.is_list(def.fallback()),
}
}

fn is_list_ref(&self, name: &TypeName) -> bool {
let ctx = &self.types[name];

match &ctx.def {
TypeDefinition::Alias(def) => self.is_list(def.alias()),
TypeDefinition::Enum(_) | TypeDefinition::Object(_) | TypeDefinition::Union(_) => false,
}
}

pub fn is_set(&self, def: &Type) -> bool {
match def {
Type::Set(_) => true,
Type::Primitive(_) | Type::Optional(_) | Type::List(_) | Type::Map(_) => false,
Type::Reference(def) => self.is_set_ref(def),
Type::External(def) => self.is_set(def.fallback()),
}
}

fn is_set_ref(&self, name: &TypeName) -> bool {
let ctx = &self.types[name];

match &ctx.def {
TypeDefinition::Alias(def) => self.is_set(def.alias()),
TypeDefinition::Enum(_) | TypeDefinition::Object(_) | TypeDefinition::Union(_) => false,
}
}

pub fn docs(&self, docs: Option<&Documentation>) -> TokenStream {
match docs {
Some(docs) => {
Expand Down Expand Up @@ -885,6 +921,16 @@ impl Context {

quote!(#(#components::)* #other_type_name)
}

pub fn is_safe_arg(&self, ty: &Type) -> bool {
match ty {
Type::External(def) => {
let name = def.external_reference();
name.package() == "com.palantir.logsafe" && name.name() == "SafeArg"
}
_ => false,
}
}
}

pub enum SetterBounds {
Expand Down
45 changes: 32 additions & 13 deletions conjure-codegen/src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn generate(ctx: &Context, def: &EnumDefinition) -> TokenStream {
quote! {
use conjure_object::serde::{ser, de};
use std::fmt;
use std::str;

#enum_
#unknown
Expand Down Expand Up @@ -63,35 +64,32 @@ fn generate_enum(ctx: &Context, def: &EnumDefinition) -> TokenStream {
quote!(#name::Unknown(v) => &*v,)
};

let visit_str_arms = def.values().iter().map(|v| {
let from_str_arms = def.values().iter().map(|v| {
let value = v.value();
let variant = ctx.type_name(value);
quote! {
#value => #ok(#name::#variant),
}
});

let values = def.values().iter().map(EnumValueDefinition::value);
let unknown_variant_error = quote! {
#err(de::Error::unknown_variant(v, &[#(#values, )*]))
};

let visit_str_other = if ctx.exhaustive() {
let from_str_other = if ctx.exhaustive() {
quote! {
v => #unknown_variant_error,
_ => #err(conjure_object::plain::ParseEnumError::new()),
}
} else {
quote! {
v => {
if conjure_object::private::valid_enum_variant(v) {
#ok(#name::Unknown(#unknown(v.to_string().into_boxed_str())))
} else {
#unknown_variant_error
#err(conjure_object::plain::ParseEnumError::new())
}
}
}
};

let values = def.values().iter().map(EnumValueDefinition::value);

quote! {
#root_docs
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -125,6 +123,27 @@ fn generate_enum(ctx: &Context, def: &EnumDefinition) -> TokenStream {
}
}

impl str::FromStr for #name {
type Err = conjure_object::plain::ParseEnumError;

#[inline]
fn from_str(v: &str) -> #result<#name, conjure_object::plain::ParseEnumError> {
match v {
#(#from_str_arms)*
#from_str_other
}
}
}

impl conjure_object::FromPlain for #name {
type Err = conjure_object::plain::ParseEnumError;

#[inline]
fn from_plain(v: &str) -> #result<#name, conjure_object::plain::ParseEnumError> {
v.parse()
}
}

impl ser::Serialize for #name {
fn serialize<S>(&self, s: S) -> #result<S::Ok, S::Error>
where
Expand All @@ -149,16 +168,16 @@ fn generate_enum(ctx: &Context, def: &EnumDefinition) -> TokenStream {
type Value = #name;

fn expecting(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
fmt.write_str("string")
fmt.write_str("a string")
}

fn visit_str<E>(self, v: &str) -> #result<#name, E>
where
E: de::Error,
{
match v {
#(#visit_str_arms)*
#visit_str_other
match v.parse() {
#ok(e) => Ok(e),
#err(_) => #err(de::Error::unknown_variant(v, &[#(#values, )*])),
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion conjure-codegen/src/example_types/another/mod.rs

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

0 comments on commit 971f07e

Please sign in to comment.