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

[Merged by Bors] - improvement: add helper method to get utf8 from data #3236

Closed
wants to merge 1 commit into from

Conversation

sehz
Copy link
Contributor

@sehz sehz commented May 8, 2023

Make it easier to get the utf8 string from record data, especially for consumers. This helps unnecessary conversion to owned unless it is necessary. Replaced existing examples with improved patterns to reduce unnecessary allocations and establish best practices for string conversion.

Also, add get_key() and get_value() to ConsumerRecord, which should be used rather than the existing value(), which returns &[u8]. Minor clean-up to remove unnecessary abstractions

Before:

while let Some(Ok(record)) = stream.next().await {
        let string = String::from_utf8_lossy(record.value());
        println!("{string}");
 }

After:

while let Some(Ok(record)) = stream.next().await {
      println!("{}", record.get_value().as_utf8_lossy_string());
}

remove unnecessary generic signature for consumerrecord
@sehz sehz added this to the 0.10.9 milestone May 8, 2023
@sehz
Copy link
Contributor Author

sehz commented May 8, 2023

bors r+

@ajhunyady
Copy link
Contributor

I ran into this issue and was looking for get_value(). We also need additional convenience methods for reading & writing.
This is very complex:

fn json_array_to_records(record: &Record) -> Result<Vec<(Option<RecordData>, RecordData)>> {
    let array = serde_json::from_slice::<Vec<serde_json::Value>>(record.value.as_ref())?;

    // Convert each JSON value from the array back into a JSON string
    let strings: Vec<String> = array
        .into_iter()
        .map(|value| serde_json::to_string(&value))
        .collect::<core::result::Result<_, _>>()?;

    // Create one record from each JSON string to send
    let kvs: Vec<(Option<RecordData>, RecordData)> = strings
        .into_iter()
        .map(|s| (None, RecordData::from(s)))
        .collect();

    Ok(kvs)
}

bors bot pushed a commit that referenced this pull request May 8, 2023
Make it easier to get the utf8 string from record data, especially for consumers.   This helps unnecessary conversion to owned unless it is necessary.   Replaced existing examples with improved patterns to reduce unnecessary allocations and establish best practices for string conversion.  

Also, add  `get_key()` and `get_value()` to ConsumerRecord, which should be used rather than the existing `value()`, which returns `&[u8]`.   Minor clean-up to remove unnecessary abstractions 


Before:
```
while let Some(Ok(record)) = stream.next().await {
        let string = String::from_utf8_lossy(record.value());
        println!("{string}");
 }
```

After:
```
while let Some(Ok(record)) = stream.next().await {
      println!("{}", record.get_value().as_utf8_lossy_string());
}
```
@bors
Copy link

bors bot commented May 8, 2023

Pull request successfully merged into master.

Build succeeded!

The publicly hosted instance of bors-ng is deprecated and will go away soon.

If you want to self-host your own instance, instructions are here.
For more help, visit the forum.

If you want to switch to GitHub's built-in merge queue, visit their help page.

@bors bors bot changed the title improvement: add helper method to get utf8 from data [Merged by Bors] - improvement: add helper method to get utf8 from data May 8, 2023
@bors bors bot closed this May 8, 2023
@sehz
Copy link
Contributor Author

sehz commented May 8, 2023

Create separate issue for that

@sehz sehz mentioned this pull request May 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants