Skip to content

Commit

Permalink
feat(go): generate new Size interface
Browse files Browse the repository at this point in the history
The newly defined `Size` interface allows to calculate the encoded size
of an instance and pre-allocate a byte vector. It can be helful for
significantly large payloads that would do several re-allocations of a
buffer otherwise.
  • Loading branch information
dnaka91 committed Dec 11, 2023
1 parent 6a8ea12 commit 5cc3c2f
Show file tree
Hide file tree
Showing 25 changed files with 981 additions and 6 deletions.
14 changes: 10 additions & 4 deletions crates/stef-go/src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use stef_parser::{
Schema, Struct, Type, TypeAlias, Variant,
};

use crate::{decode, encode, Opts, Output};
use crate::{decode, encode, size, Opts, Output};

/// Take a single schema and convert it into Go source code (which can result in multiple files).
#[must_use]
Expand Down Expand Up @@ -68,9 +68,10 @@ fn render_definition<'a>(buf: &mut String, definition: &'a Definition<'_>) -> Op
.unwrap();
writeln!(
buf,
"\n{}\n{}",
"\n{}\n{}\n{}",
encode::RenderStruct(s),
decode::RenderStruct(s)
decode::RenderStruct(s),
size::RenderStruct(s),
)
.unwrap();
}
Expand Down Expand Up @@ -709,7 +710,7 @@ impl Display for RenderEnumVariant<'_> {

write!(
f,
"\n{}\n{}",
"\n{}\n{}\n{}",
encode::RenderEnumVariant {
enum_name: self.enum_name,
generics: self.generics,
Expand All @@ -720,6 +721,11 @@ impl Display for RenderEnumVariant<'_> {
generics: self.generics,
variant: self.variant,
},
size::RenderEnumVariant {
enum_name: self.enum_name,
generics: self.generics,
variant: self.variant,
},
)
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/stef-go/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Display for RenderFields<'_> {
writeln!(
f,
"\tw = buf.EncodeFieldOption[{}](w, {}, &v.F{idx}, func (w []byte, v \
{0}) [] byte {{\n\t\treturn {}\n\t}})",
{0}) []byte {{\n\t\treturn {}\n\t}})",
definition::RenderType(ty),
field.id.get(),
RenderType {
Expand Down
1 change: 1 addition & 0 deletions crates/stef-go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub use definition::render_schema;
mod decode;
mod definition;
mod encode;
mod size;

/// Options for the code generator that can modify the way the code is generated.
#[derive(Default)]
Expand Down
Loading

0 comments on commit 5cc3c2f

Please sign in to comment.