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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Manage empty props object #7

Merged
merged 9 commits into from
Oct 6, 2023
Merged

fix: Manage empty props object #7

merged 9 commits into from
Oct 6, 2023

Conversation

taorepoara
Copy link
Contributor

@taorepoara taorepoara commented Oct 6, 2023

About this PR

When no props is defined an empty object is sent in the view and listener requests.

When a view handle a props struct as optional and view is called without props, the empty props object is sent and we have an error while parsing to the expected props struct.

To resolve this, if the props object is empty, we return None.

How to test my changes

Create a view that can have an optional props object:

use lenra_app::{
    components::lenra::text,
    props,
    view::{ViewParams, ViewResponse, ViewResponseGenerator},
    Result,
};
use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize)]
pub struct TestProps {
    pub val: String,

props!(TestProps);

pub fn test(params: ViewParams<Value, TestProps>) -> Result<ViewResponse> {
    let val = if let Some(props) = params.props {
        props.val
    } else {
        "default value".into()
    };
    let result: LenraComponent = text(val).into();
    Ok(result.gen())
}

Define this view in two routes, one with the props and one without:

vec![
Route::builder()
      .path(format!("/test-props", path))
      .view(
          view("test").props(Some(
              TestProps {
                  val: "test value".into(),
              }
              .try_into()
              .unwrap(),
          )),
      )
      .try_into()
      .unwrap(),
Route::builder()
      .path(format!("/test-props", path))
      .view(view("test"))
      .try_into()
      .unwrap(),
]

Checklist

  • I didn't over-scope my PR
  • My PR title matches the commit convention
  • I did not include breaking changes
  • I made my own code-review before requesting one

I included unit tests that cover my changes

  • 馃憤 yes
  • 馃檯 no, because they aren't needed
  • 馃檵 no, because I need help

I added/updated the documentation about my changes

  • 馃摐 README.md
  • 馃摃 docs/*.md
  • 馃摀 docs.lenra.io
  • 馃檯 no documentation needed

taorepoara and others added 6 commits April 24, 2023 16:55
* feat: Init project

* fix: Imports

* feat: Abstraction

* feat: Simplify

* fix: Visibility

* ci: Init CI

* fix: Remove useless code

* build: No artifacts

* docs: README and default values (need other changes)

* fix: cleanup

* docs: Remove test part

* Update README.md

---------

Co-authored-by: Jonas Martinez <36544012+jonas-martinez@users.noreply.github.com>
@taorepoara taorepoara added the bug Something isn't working label Oct 6, 2023
@taorepoara taorepoara self-assigned this Oct 6, 2023
@taorepoara taorepoara marked this pull request as ready for review October 6, 2023 10:43
@taorepoara taorepoara merged commit 8a87517 into main Oct 6, 2023
@taorepoara taorepoara deleted the fix-from-value branch October 6, 2023 13:14
@github-actions
Copy link

github-actions bot commented Oct 6, 2023

馃帀 This PR is included in version 1.0.1 馃帀

The release is available on GitHub release

Your semantic-release bot 馃摝馃殌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working released
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

2 participants