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

encode value #114

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ categories = ["encoding", "parsing"]
license = "MIT/Apache-2.0"
edition = "2021"

[workspace]
resolver = "2"

[features]
native_tls = ["reqwest/native-tls"]
rustls_tls = ["reqwest/rustls-tls"]
Expand Down Expand Up @@ -44,7 +47,7 @@ features = ["derive"]
version = "^1.0"

[dependencies.apache-avro]
version = "^0.16"
version = "0.17.0"
optional = true

[dependencies.bytes]
Expand Down
14 changes: 13 additions & 1 deletion src/async_impl/avro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ use crate::async_impl::schema_registry::{
get_referenced_schema, get_schema_by_id_and_type, get_schema_by_subject, SrSettings,
};
use crate::avro_common::{
get_name, item_to_bytes, replace_reference, values_to_bytes, AvroSchema, DecodeResult,
get_name, item_to_bytes, replace_reference, values_to_bytes, AvroSchema, DecodeResult, record_to_bytes,
DecodeResultWithSchema,
};
use crate::error::SRCError;
Expand Down Expand Up @@ -532,6 +532,18 @@ impl<'a> AvroEncoder<'a> {
item_to_bytes(&schema, item)
}

pub async fn encode_value(
&self,
item: Value,
subject_name_strategy: &SubjectNameStrategy,
) -> Result<Vec<u8>, SRCError> {
let key = subject_name_strategy.get_subject()?;
let schema = self
.get_schema_and_id(&key, subject_name_strategy.clone())
.await?;
record_to_bytes(&schema, item)
}

pub async fn get_schema_and_id(
&self,
key: &str,
Expand Down
9 changes: 9 additions & 0 deletions src/async_impl/easy_avro.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ impl EasyAvroEncoder {
.encode_struct(item, subject_name_strategy)
.await
}

pub async fn encode_value(
&self,
item: Value,
subject_name_strategy: &SubjectNameStrategy,
) -> Result<Vec<u8>, SRCError> {
self.encoder.encode_value(item, subject_name_strategy).await
}

pub async fn get_schema_and_id(
&self,
key: &str,
Expand Down
9 changes: 9 additions & 0 deletions src/avro_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ pub(crate) fn values_to_bytes(
avro_schema: &AvroSchema,
values: Vec<(&str, Value)>,
) -> Result<Vec<u8>, SRCError> {

let mut record = match Record::new(&avro_schema.parsed) {
Some(v) => v,
None => {
Expand All @@ -135,6 +136,7 @@ pub(crate) fn values_to_bytes(
to_bytes(avro_schema, Value::from(record))
}


/// Using the schema with an item implementing serialize the item will be correctly deserialized
/// according to the avro specification.
pub(crate) fn item_to_bytes(
Expand All @@ -153,6 +155,13 @@ pub(crate) fn item_to_bytes(
}
}

pub(crate) fn record_to_bytes(
avro_schema: &AvroSchema,
item: Value,
) -> Result<Vec<u8>, SRCError> {
to_bytes(avro_schema, item)
}

pub(crate) fn get_name(schema: &Schema) -> Option<Name> {
match schema {
Schema::Record(schema) => Some(schema.name.clone()),
Expand Down