Skip to content

Commit

Permalink
default implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik committed Oct 6, 2024
1 parent edb8d1d commit c5f8e9c
Show file tree
Hide file tree
Showing 7 changed files with 220 additions and 17 deletions.
34 changes: 32 additions & 2 deletions varnish-macros/snapshots/[email protected]

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions varnish-macros/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import types from "path/to/libtypes.so";

### Function `VOID type_bool(BOOL _v)`

### Function `VOID type_bool_dflt(BOOL _v = true)`

### Function `VOID opt_bool([BOOL _v])`

### Function `BOOL to_bool()`
Expand All @@ -44,6 +46,8 @@ import types from "path/to/libtypes.so";

### Function `VOID type_f64(REAL _v)`

### Function `VOID type_f64_dflt(REAL _v = 42.3)`

### Function `VOID opt_f64([REAL _v])`

### Function `REAL to_f64()`
Expand All @@ -52,6 +56,8 @@ import types from "path/to/libtypes.so";

### Function `VOID type_i64(INT _v)`

### Function `VOID type_i64_dflt(INT _v = 10)`

### Function `VOID opt_i64([INT _v])`

### Function `INT to_i64()`
Expand All @@ -62,9 +68,9 @@ import types from "path/to/libtypes.so";

### Function `VOID opt_str([STRING _v])`

### Function `VOID type_str_dflt(STRING _v)`
### Function `VOID type_str_dflt(STRING _v = "baz")`

### Function `VOID opt_str_dflt([STRING _v])`
### Function `VOID opt_str_dflt([STRING _v] = "baz")`

### Function `STRING to_str()`

Expand Down
68 changes: 67 additions & 1 deletion varnish-macros/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ VMOD_JSON_SPEC
"1.0",
"types",
"Vmod_vmod_types_Func",
"ddf5fae8ddbc3c73e374213308c805bffb2a36442bb7d60d1b0cc17938ae8b18",
"305d7e5bb207b939306ed27283d79808276446ee4e23dc9f083d4cf73c54d751",
"Varnish (version) (hash)",
"0",
"0"
Expand All @@ -33,6 +33,11 @@ typedef VCL_VOID td_vmod_types_type_bool(
VCL_BOOL
);

typedef VCL_VOID td_vmod_types_type_bool_dflt(
VRT_CTX,
VCL_BOOL
);

struct arg_vmod_types_opt_bool {
char valid__v;
VCL_BOOL _v;
Expand Down Expand Up @@ -79,6 +84,11 @@ typedef VCL_VOID td_vmod_types_type_f64(
VCL_REAL
);

typedef VCL_VOID td_vmod_types_type_f64_dflt(
VRT_CTX,
VCL_REAL
);

struct arg_vmod_types_opt_f64 {
char valid__v;
VCL_REAL _v;
Expand All @@ -102,6 +112,11 @@ typedef VCL_VOID td_vmod_types_type_i64(
VCL_INT
);

typedef VCL_VOID td_vmod_types_type_i64_dflt(
VRT_CTX,
VCL_INT
);

struct arg_vmod_types_opt_i64 {
char valid__v;
VCL_INT _v;
Expand Down Expand Up @@ -238,6 +253,7 @@ struct Vmod_vmod_types_Func {
td_vmod_types_to_res_void_err *f_to_res_void_err;
td_vmod_types_to_res_str_err *f_to_res_str_err;
td_vmod_types_type_bool *f_type_bool;
td_vmod_types_type_bool_dflt *f_type_bool_dflt;
td_vmod_types_opt_bool *f_opt_bool;
td_vmod_types_to_bool *f_to_bool;
td_vmod_types_to_res_bool *f_to_res_bool;
Expand All @@ -246,10 +262,12 @@ struct Vmod_vmod_types_Func {
td_vmod_types_to_duration *f_to_duration;
td_vmod_types_to_res_duration *f_to_res_duration;
td_vmod_types_type_f64 *f_type_f64;
td_vmod_types_type_f64_dflt *f_type_f64_dflt;
td_vmod_types_opt_f64 *f_opt_f64;
td_vmod_types_to_f64 *f_to_f64;
td_vmod_types_to_res_f64 *f_to_res_f64;
td_vmod_types_type_i64 *f_type_i64;
td_vmod_types_type_i64_dflt *f_type_i64_dflt;
td_vmod_types_opt_i64 *f_opt_i64;
td_vmod_types_to_i64 *f_to_i64;
td_vmod_types_to_res_i64 *f_to_res_i64;
Expand Down Expand Up @@ -327,6 +345,22 @@ static struct Vmod_vmod_types_Func Vmod_vmod_types_Func;"
]
]
],
[
"$FUNC",
"type_bool_dflt",
[
[
"VOID"
],
"Vmod_vmod_types_Func.f_type_bool_dflt",
"",
[
"BOOL",
"_v",
"true"
]
]
],
[
"$FUNC",
"opt_bool",
Expand Down Expand Up @@ -437,6 +471,22 @@ static struct Vmod_vmod_types_Func Vmod_vmod_types_Func;"
]
]
],
[
"$FUNC",
"type_f64_dflt",
[
[
"VOID"
],
"Vmod_vmod_types_Func.f_type_f64_dflt",
"",
[
"REAL",
"_v",
"42.3"
]
]
],
[
"$FUNC",
"opt_f64",
Expand Down Expand Up @@ -492,6 +542,22 @@ static struct Vmod_vmod_types_Func Vmod_vmod_types_Func;"
]
]
],
[
"$FUNC",
"type_i64_dflt",
[
[
"VOID"
],
"Vmod_vmod_types_Func.f_type_i64_dflt",
"",
[
"INT",
"_v",
"10"
]
]
],
[
"$FUNC",
"opt_i64",
Expand Down
69 changes: 69 additions & 0 deletions varnish-macros/snapshots/[email protected]
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ VmodInfo {
Default,
),
},
FuncInfo {
func_type: Function,
ident: "type_bool_dflt",
docs: "",
has_optional_args: false,
args: [
ParamTypeInfo {
ident: "_v",
docs: "",
idx: 0,
ty: Value(
ParamInfo {
is_optional: false,
default: Bool(true),
ty_info: Bool,
},
),
},
],
returns: Value(
Default,
),
},
FuncInfo {
func_type: Function,
ident: "opt_bool",
Expand Down Expand Up @@ -211,6 +234,29 @@ VmodInfo {
Default,
),
},
FuncInfo {
func_type: Function,
ident: "type_f64_dflt",
docs: "",
has_optional_args: false,
args: [
ParamTypeInfo {
ident: "_v",
docs: "",
idx: 0,
ty: Value(
ParamInfo {
is_optional: false,
default: Number(42.3),
ty_info: F64,
},
),
},
],
returns: Value(
Default,
),
},
FuncInfo {
func_type: Function,
ident: "opt_f64",
Expand Down Expand Up @@ -284,6 +330,29 @@ VmodInfo {
Default,
),
},
FuncInfo {
func_type: Function,
ident: "type_i64_dflt",
docs: "",
has_optional_args: false,
args: [
ParamTypeInfo {
ident: "_v",
docs: "",
idx: 0,
ty: Value(
ParamInfo {
is_optional: false,
default: Number(10),
ty_info: I64,
},
),
},
],
returns: Value(
Default,
),
},
FuncInfo {
func_type: Function,
ident: "opt_i64",
Expand Down
3 changes: 3 additions & 0 deletions varnish-macros/src/gen_docs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ fn fn_sig(func: &FuncInfo, user_args: &Vec<(&ParamTypeInfo, &ParamInfo)>) -> Str
wrt!(res, ", ");
}
wrt!(res, "{}", bracketed_name(arg, ty));
if !ty.default.is_null() {
wrt!(res, " = {}", ty.default);
}
}
wrt!(res, ")");
if is_md_txt {
Expand Down
50 changes: 38 additions & 12 deletions varnish-macros/src/parser_args.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use darling::ast::NestedMeta;
use darling::util::parse_expr::preserve_str_literal;
use darling::FromMeta;
use serde_json::Value;
use syn::Type::Tuple;
use syn::{FnArg, GenericArgument, Meta, Pat, PatType, Type, TypeParamBound};
use syn::{Expr, ExprLit, FnArg, GenericArgument, Lit, Meta, Pat, PatType, Type, TypeParamBound};

use crate::errors::error;
use crate::model::FuncType::{Constructor, Event, Function, Method};
Expand Down Expand Up @@ -37,10 +38,10 @@ impl FuncStatus {
}

/// Represents function parameter configuration
#[derive(Default, Debug, FromMeta)]
#[darling(default)]
pub struct ArgConfig {
pub default: Option<String>,
#[derive(Debug, FromMeta)]
struct ArgConfig {
#[darling(with = preserve_str_literal)]
pub default: Expr,
}

impl ParamTypeInfo {
Expand Down Expand Up @@ -204,14 +205,39 @@ impl ParamType {
let Meta::List(arg) = arg.meta else {
Err(error(&pat_ty, "Unexpected #[arg(...)] attribute"))?
};
if !matches!(arg_type, ParamTy::Str) {
Err(error(
&pat_ty,
"Only `&str` arguments can have a default value for now. Parsing other types is not yet implemented",
))?;
}
let arg = NestedMeta::parse_meta_list(arg.tokens)?;
Ok(ArgConfig::from_list(&arg)?.default.into())
let arg = ArgConfig::from_list(&arg)?;
let Expr::Lit(ExprLit { lit, .. }) = arg.default else {
Err(error(&pat_ty, "Default value must be a literal value"))?
};

macro_rules! only {
($pat:pat, $msg:literal) => {
if !matches!(arg_type, $pat) {
Err(error(&pat_ty, $msg))?;
}
};
}

Ok(match lit {
Lit::Str(v) => {
only! { ParamTy::Str, "Only `&str` arguments can have a default string value" };
Value::String(v.value())
}
Lit::Int(v) => {
only! { ParamTy::I64, "Only `i64` arguments can have a default integer value" };
serde_json::from_str(&v.to_string()).unwrap()
}
Lit::Float(v) => {
only! { ParamTy::F64, "Only `f64` arguments can have a default float value" };
serde_json::from_str(&v.to_string()).unwrap()
}
Lit::Bool(v) => {
only! { ParamTy::Bool, "Only `bool` arguments can have a default boolean value" };
Value::Bool(v.value)
}
_ => Err(error(&pat_ty, "Unrecognized literal value"))?,
})
}
}

Expand Down
3 changes: 3 additions & 0 deletions varnish-macros/tests/pass/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod types {

// bool
pub fn type_bool(_v: bool) {}
pub fn type_bool_dflt(#[arg(default = true)] _v: bool) {}
pub fn opt_bool(_v: Option<bool>) {}
pub fn to_bool() -> bool {
panic!()
Expand All @@ -45,6 +46,7 @@ mod types {

// f64
pub fn type_f64(_v: f64) {}
pub fn type_f64_dflt(#[arg(default = 42.3)] _v: f64) {}
pub fn opt_f64(_v: Option<f64>) {}
pub fn to_f64() -> f64 {
panic!()
Expand All @@ -55,6 +57,7 @@ mod types {

// i64
pub fn type_i64(_v: i64) {}
pub fn type_i64_dflt(#[arg(default = 10)] _v: i64) {}
pub fn opt_i64(_v: Option<i64>) {}
pub fn to_i64() -> i64 {
panic!()
Expand Down

0 comments on commit c5f8e9c

Please sign in to comment.