difftreelog
feat rename/flatten field in derive(Typed)
in: master
4 files changed
crates/jrsonnet-evaluator/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/lib.rs
+++ b/crates/jrsonnet-evaluator/src/lib.rs
@@ -1280,7 +1280,8 @@
#[derive(Typed, PartialEq, Debug)]
struct MyTyped {
a: u32,
- b: String,
+ #[typed(rename = "b")]
+ c: String,
}
#[test]
@@ -1293,7 +1294,7 @@
typed,
MyTyped {
a: 14,
- b: "Hello, world!".to_string()
+ c: "Hello, world!".to_string()
}
);
es.settings_mut().globals.insert(
crates/jrsonnet-evaluator/src/typed/conversions.rsdiffbeforeafterboth--- a/crates/jrsonnet-evaluator/src/typed/conversions.rs
+++ b/crates/jrsonnet-evaluator/src/typed/conversions.rs
@@ -8,9 +8,19 @@
error::{Error::*, LocError, Result},
throw,
typed::CheckType,
- ArrValue, FuncVal, IndexableVal, ObjValue, Val,
+ ArrValue, FuncVal, IndexableVal, ObjValue, ObjValueBuilder, Val,
};
+pub trait TypedObj: Typed {
+ fn serialize(self, out: &mut ObjValueBuilder) -> Result<()>;
+ fn parse(obj: &ObjValue) -> Result<Self>;
+ fn into_object(self) -> Result<ObjValue> {
+ let mut builder = ObjValueBuilder::new();
+ self.serialize(&mut builder)?;
+ Ok(builder.build())
+ }
+}
+
pub trait Typed: TryFrom<Val, Error = LocError> + TryInto<Val, Error = LocError> {
const TYPE: &'static ComplexValType;
}
crates/jrsonnet-evaluator/src/typed/mod.rsdiffbeforeafterboth1use std::{fmt::Display, rc::Rc};23mod conversions;4pub use conversions::*;56use crate::{7 error::{Error, LocError, Result},8 push_description_frame, Val,9};10use gcmodule::Trace;11pub use jrsonnet_types::{ComplexValType, ValType};12use thiserror::Error;1314#[derive(Debug, Error, Clone, Trace)]15pub enum TypeError {16 #[error("expected {0}, got {1}")]17 ExpectedGot(ComplexValType, ValType),18 #[error("missing property {0} from {1:?}")]19 MissingProperty(#[skip_trace] Rc<str>, ComplexValType),20 #[error("every failed from {0}:\n{1}")]21 UnionFailed(ComplexValType, TypeLocErrorList),22 #[error(23 "number out of bounds: {0} not in {}..{}",24 .1.map(|v|v.to_string()).unwrap_or_else(|| "".to_owned()),25 .2.map(|v|v.to_string()).unwrap_or_else(|| "".to_owned()),26 )]27 BoundsFailed(f64, Option<f64>, Option<f64>),28}29impl From<TypeError> for LocError {30 fn from(e: TypeError) -> Self {31 Error::TypeError(e.into()).into()32 }33}3435#[derive(Debug, Clone, Trace)]36pub struct TypeLocError(Box<TypeError>, ValuePathStack);37impl From<TypeError> for TypeLocError {38 fn from(e: TypeError) -> Self {39 Self(Box::new(e), ValuePathStack(Vec::new()))40 }41}42impl From<TypeLocError> for LocError {43 fn from(e: TypeLocError) -> Self {44 Error::TypeError(e).into()45 }46}47impl Display for TypeLocError {48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {49 write!(f, "{}", self.0)?;50 if !(self.1).0.is_empty() {51 write!(f, " at {}", self.1)?;52 }53 Ok(())54 }55}5657#[derive(Debug, Clone, Trace)]58pub struct TypeLocErrorList(Vec<TypeLocError>);59impl Display for TypeLocErrorList {60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {61 use std::fmt::Write;62 let mut out = String::new();63 for (i, err) in self.0.iter().enumerate() {64 if i != 0 {65 writeln!(f)?;66 }67 out.clear();68 write!(out, "{}", err)?;6970 for (i, line) in out.lines().enumerate() {71 if line.trim().is_empty() {72 continue;73 }74 if i != 0 {75 writeln!(f)?;76 write!(f, " ")?;77 } else {78 write!(f, " - ")?;79 }80 write!(f, "{}", line)?;81 }82 }83 Ok(())84 }85}8687fn push_type_description(88 error_reason: impl Fn() -> String,89 path: impl Fn() -> ValuePathItem,90 item: impl Fn() -> Result<()>,91) -> Result<()> {92 push_description_frame(error_reason, || match item() {93 Ok(_) => Ok(()),94 Err(mut e) => {95 if let Error::TypeError(e) = &mut e.error_mut() {96 (e.1).0.push(path())97 }98 Err(e)99 }100 })101}102103// TODO: check_fast for fast path of union type checking104pub trait CheckType {105 fn check(&self, value: &Val) -> Result<()>;106}107108impl CheckType for ValType {109 fn check(&self, value: &Val) -> Result<()> {110 let got = value.value_type();111 if got != *self {112 let loc_error: TypeLocError = TypeError::ExpectedGot((*self).into(), got).into();113 return Err(loc_error.into());114 }115 Ok(())116 }117}118119#[derive(Clone, Debug, Trace)]120enum ValuePathItem {121 Field(#[skip_trace] Rc<str>),122 Index(u64),123}124impl Display for ValuePathItem {125 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {126 match self {127 Self::Field(name) => write!(f, ".{:?}", name)?,128 Self::Index(idx) => write!(f, "[{}]", idx)?,129 }130 Ok(())131 }132}133134#[derive(Clone, Debug, Trace)]135struct ValuePathStack(Vec<ValuePathItem>);136impl Display for ValuePathStack {137 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {138 write!(f, "self")?;139 for elem in self.0.iter().rev() {140 write!(f, "{}", elem)?;141 }142 Ok(())143 }144}145146impl CheckType for ComplexValType {147 fn check(&self, value: &Val) -> Result<()> {148 match self {149 Self::Any => Ok(()),150 Self::Simple(s) => s.check(value),151 Self::Char => match value {152 Val::Str(s) if s.len() == 1 || s.chars().count() == 1 => Ok(()),153 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),154 },155 Self::BoundedNumber(from, to) => {156 if let Val::Num(n) = value {157 if from.map(|from| from > *n).unwrap_or(false)158 || to.map(|to| to < *n).unwrap_or(false)159 {160 return Err(TypeError::BoundsFailed(*n, *from, *to).into());161 }162 Ok(())163 } else {164 Err(TypeError::ExpectedGot(self.clone(), value.value_type()).into())165 }166 }167 Self::Array(elem_type) => match value {168 Val::Arr(a) => {169 for (i, item) in a.iter().enumerate() {170 push_type_description(171 || format!("array index {}", i),172 || ValuePathItem::Index(i as u64),173 || elem_type.check(&item.clone()?),174 )?;175 }176 Ok(())177 }178 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),179 },180 Self::ArrayRef(elem_type) => match value {181 Val::Arr(a) => {182 for (i, item) in a.iter().enumerate() {183 push_type_description(184 || format!("array index {}", i),185 || ValuePathItem::Index(i as u64),186 || elem_type.check(&item.clone()?),187 )?;188 }189 Ok(())190 }191 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),192 },193 Self::ObjectRef(elems) => match value {194 Val::Obj(obj) => {195 for (k, v) in elems.iter() {196 if let Some(got_v) = obj.get((*k).into())? {197 push_type_description(198 || format!("property {}", k),199 || ValuePathItem::Field((*k).into()),200 || v.check(&got_v),201 )?202 } else {203 return Err(204 TypeError::MissingProperty((*k).into(), self.clone()).into()205 );206 }207 }208 Ok(())209 }210 v => Err(TypeError::ExpectedGot(self.clone(), v.value_type()).into()),211 },212 Self::Union(types) => {213 let mut errors = Vec::new();214 for ty in types.iter() {215 match ty.check(value) {216 Ok(()) => {217 return Ok(());218 }219 Err(e) => match e.error() {220 Error::TypeError(e) => errors.push(e.clone()),221 _ => return Err(e),222 },223 }224 }225 Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())226 }227 Self::UnionRef(types) => {228 let mut errors = Vec::new();229 for ty in types.iter() {230 match ty.check(value) {231 Ok(()) => {232 return Ok(());233 }234 Err(e) => match e.error() {235 Error::TypeError(e) => errors.push(e.clone()),236 _ => return Err(e),237 },238 }239 }240 Err(TypeError::UnionFailed(self.clone(), TypeLocErrorList(errors)).into())241 }242 Self::Sum(types) => {243 for ty in types.iter() {244 ty.check(value)?245 }246 Ok(())247 }248 Self::SumRef(types) => {249 for ty in types.iter() {250 ty.check(value)?251 }252 Ok(())253 }254 }255 }256}crates/jrsonnet-macros/src/lib.rsdiffbeforeafterboth--- a/crates/jrsonnet-macros/src/lib.rs
+++ b/crates/jrsonnet-macros/src/lib.rs
@@ -1,10 +1,38 @@
+use proc_macro2::TokenStream;
use quote::{quote, quote_spanned};
use syn::{
- parenthesized, parse::Parse, parse_macro_input, punctuated::Punctuated, spanned::Spanned,
- token::Comma, DeriveInput, FnArg, GenericArgument, Ident, ItemFn, Pat, PatType, Path,
- PathArguments, Token, Type,
+ parenthesized,
+ parse::{Parse, ParseStream},
+ parse_macro_input,
+ punctuated::Punctuated,
+ spanned::Spanned,
+ token::Comma,
+ Attribute, DeriveInput, Error, FnArg, GenericArgument, Ident, ItemFn, LitStr, Pat, PatType,
+ Path, PathArguments, Result, Token, Type,
};
+fn parse_attr<A: Parse, I>(attrs: &[Attribute], ident: I) -> Result<Option<A>>
+where
+ Ident: PartialEq<I>,
+{
+ let attrs = attrs
+ .iter()
+ .filter(|a| a.path.is_ident(&ident))
+ .collect::<Vec<_>>();
+ if attrs.len() > 1 {
+ return Err(Error::new(
+ attrs[1].span(),
+ "this attribute may be specified only once",
+ ));
+ } else if attrs.is_empty() {
+ return Ok(None);
+ }
+ let attr = attrs[0];
+ let attr = attr.parse_args::<A>()?;
+
+ Ok(Some(attr))
+}
+
fn is_location_arg(t: &PatType) -> bool {
t.attrs.iter().any(|a| a.path.is_ident("location"))
}
@@ -56,7 +84,7 @@
ty: Type,
}
impl Parse for Field {
- fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+ fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(Self {
name: input.parse()?,
_colon: input.parse()?,
@@ -67,13 +95,22 @@
mod kw {
syn::custom_keyword!(fields);
+ syn::custom_keyword!(rename);
+ syn::custom_keyword!(flatten);
+}
+
+struct EmptyAttr;
+impl Parse for EmptyAttr {
+ fn parse(input: ParseStream) -> Result<Self> {
+ Ok(Self)
+ }
}
struct BuiltinAttrs {
fields: Vec<Field>,
}
impl Parse for BuiltinAttrs {
- fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
+ fn parse(input: ParseStream) -> syn::Result<Self> {
if input.is_empty() {
return Ok(Self { fields: Vec::new() });
}
@@ -255,7 +292,172 @@
.into()
}
-#[proc_macro_derive(Typed)]
+#[derive(Default)]
+struct TypedAttr {
+ rename: Option<String>,
+ flatten: bool,
+}
+impl Parse for TypedAttr {
+ fn parse(input: ParseStream) -> syn::Result<Self> {
+ let mut out = Self::default();
+ loop {
+ let lookahead = input.lookahead1();
+ if lookahead.peek(kw::rename) {
+ input.parse::<kw::rename>()?;
+ input.parse::<Token![=]>()?;
+ let name = input.parse::<LitStr>()?;
+ if out.rename.is_some() {
+ return Err(Error::new(
+ name.span(),
+ "rename attribute may only be specified once",
+ ));
+ }
+ out.rename = Some(name.value());
+ } else if lookahead.peek(kw::flatten) {
+ input.parse::<kw::flatten>()?;
+ out.flatten = true;
+ } else if input.is_empty() {
+ break;
+ } else {
+ return Err(lookahead.error());
+ }
+ if input.peek(Token![,]) {
+ input.parse::<Token![,]>()?;
+ } else {
+ break;
+ }
+ }
+ // input.parse::<kw::rename>()?;
+ // input.parse::<Token![=]>()?;
+ // let rename = input.parse::<LitStr>()?.value();
+ Ok(out)
+ }
+}
+
+struct TypedField<'f>(&'f syn::Field, TypedAttr);
+impl<'f> TypedField<'f> {
+ fn try_new(field: &'f syn::Field) -> Result<Self> {
+ let attr =
+ parse_attr::<TypedAttr, _>(&field.attrs, "typed")?.unwrap_or_else(Default::default);
+ if field.ident.is_none() {
+ return Err(Error::new(
+ field.span(),
+ "this field should appear in output object, but it has no visible name",
+ ));
+ }
+ Ok(Self(field, attr))
+ }
+ fn ident(&self) -> Ident {
+ self.0
+ .ident
+ .clone()
+ .expect("constructor disallows fields without name")
+ }
+ /// None if this field is flattened in jsonnet output
+ fn name(&self) -> Option<String> {
+ if self.1.flatten {
+ return None;
+ }
+ Some(
+ self.1
+ .rename
+ .clone()
+ .unwrap_or_else(|| self.ident().to_string()),
+ )
+ }
+
+ fn expand_shallow_field(&self) -> Option<TokenStream> {
+ if self.is_option() {
+ return None;
+ }
+ let name = self.name()?;
+ Some(quote! {
+ (#name, ComplexValType::Any)
+ })
+ }
+ fn expand_field(&self) -> Option<TokenStream> {
+ if self.is_option() {
+ return None;
+ }
+ let name = self.name()?;
+ let ty = &self.0.ty;
+ Some(quote! {
+ (#name, <#ty>::TYPE)
+ })
+ }
+ fn expand_parse(&self) -> TokenStream {
+ let ident = self.ident();
+ let ty = &self.0.ty;
+ if self.1.flatten {
+ // optional flatten is handled in same way as serde
+ return if self.is_option() {
+ quote! {
+ #ident: <#ty>::parse(&obj).ok(),
+ }
+ } else {
+ quote! {
+ #ident: <#ty>::parse(&obj)?,
+ }
+ };
+ };
+
+ let name = self.name().unwrap();
+ let value = if let Some(ty) = self.as_option() {
+ quote! {
+ if let Some(value) = obj.get(#name.into())? {
+ Some(<#ty>::try_from(vakue)?)
+ } else {
+ None
+ }
+ }
+ } else {
+ quote! {
+ <#ty>::try_from(obj.get(#name.into())?.ok_or_else(|| Error::NoSuchField(#name.into()))?)?
+ }
+ };
+
+ quote! {
+ #ident: #value,
+ }
+ }
+ fn expand_serialize(&self) -> TokenStream {
+ let ident = self.ident();
+ if let Some(name) = self.name() {
+ if self.is_option() {
+ quote! {
+ if let Some(value) = self.#ident {
+ out.member(#name.into()).value(value.try_into()?);
+ }
+ }
+ } else {
+ quote! {
+ out.member(#name.into()).value(self.#ident.try_into()?);
+ }
+ }
+ } else {
+ if self.is_option() {
+ quote! {
+ if let Some(value) = self.#ident {
+ value.serialize(out)?;
+ }
+ }
+ } else {
+ quote! {
+ self.#ident.serialize(out)?;
+ }
+ }
+ }
+ }
+
+ fn as_option(&self) -> Option<&Type> {
+ extract_type_from_option(&self.0.ty)
+ }
+ fn is_option(&self) -> bool {
+ self.as_option().is_some()
+ }
+}
+
+#[proc_macro_derive(Typed, attributes(typed))]
pub fn derive_typed(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
let input = parse_macro_input!(item as DeriveInput);
let data = match &input.data {
@@ -268,67 +470,71 @@
};
let ident = &input.ident;
+ let fields = match data
+ .fields
+ .iter()
+ .map(TypedField::try_new)
+ .collect::<Result<Vec<_>>>()
+ {
+ Ok(v) => v,
+ Err(e) => return e.to_compile_error().into(),
+ };
- let fields_def = data.fields.iter().map(|f| {
- let name = f
- .ident
- .as_ref()
- .expect("only named fields supported")
- .to_string();
- let ty = &f.ty;
- quote! {
- (#name, #ty::TYPE),
- }
- });
- let fields_parse = data.fields.iter().map(|f| {
- let ident = f.ident.as_ref().unwrap();
- let name = ident.to_string();
- let ty = &f.ty;
+ let typed = {
+ let fields = fields
+ .iter()
+ .flat_map(TypedField::expand_field)
+ .collect::<Vec<_>>();
+ let len = fields.len();
quote! {
- #ident: #ty::try_from(obj.get(#name.into())?.expect("shape is correct"))?,
+ const ITEMS: [(&'static str, &'static ComplexValType); #len] = [
+ #(#fields,)*
+ ];
+ impl Typed for #ident {
+ const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);
+ }
}
- });
- let fields_serialize = data.fields.iter().map(|f| {
- let ident = f.ident.as_ref().unwrap();
- let name = ident.to_string();
- quote! {
- out.member(#name.into()).value(self.#ident.try_into()?);
- }
- });
- let field_count = data.fields.len();
+ };
+
+ let fields_parse = fields.iter().map(TypedField::expand_parse);
+ let fields_serialize = fields.iter().map(TypedField::expand_serialize);
quote! {
const _: () = {
use ::jrsonnet_evaluator::{
- typed::{ComplexValType, Typed, CheckType},
+ typed::{ComplexValType, Typed, TypedObj, CheckType},
Val,
- error::LocError,
- obj::ObjValueBuilder,
+ error::{LocError, Error},
+ ObjValueBuilder, ObjValue,
};
- const ITEMS: [(&'static str, &'static ComplexValType); #field_count] = [
- #(#fields_def)*
- ];
- impl Typed for #ident {
- const TYPE: &'static ComplexValType = &ComplexValType::ObjectRef(&ITEMS);
+ #typed
+
+ impl #ident {
+ fn serialize(self, out: &mut ObjValueBuilder) -> Result<(), LocError> {
+ #(#fields_serialize)*
+
+ Ok(())
+ }
+ fn parse(obj: &ObjValue) -> Result<Self, LocError> {
+ Ok(Self {
+ #(#fields_parse)*
+ })
+ }
}
impl TryFrom<Val> for #ident {
type Error = LocError;
fn try_from(value: Val) -> Result<Self, Self::Error> {
- <Self as Typed>::TYPE.check(&value)?;
let obj = value.as_obj().expect("shape is correct");
-
- Ok(Self {
- #(#fields_parse)*
- })
+ Self::parse(&obj)
}
}
impl TryInto<Val> for #ident {
type Error = LocError;
fn try_into(self) -> Result<Val, Self::Error> {
let mut out = ObjValueBuilder::new();
- #(#fields_serialize)*
+ self.serialize(&mut out)?;
Ok(Val::Obj(out.build()))
}
}