[GH-ISSUE #33] Create "default" values/Builder pattern for some structs #11

Closed
opened 2026-02-27 20:22:35 +03:00 by kerem · 2 comments
Owner

Originally created by @mrcosta on GitHub (Feb 13, 2019).
Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/33

Hello,

I'm unit testing my code and in order to do it I need to create a simplified version of SimplifiedAlbum (I understand the irony here of simplified version of something already simplified ) since I need just external_urls, id, name.

An example of one that I'm creating:

SimplifiedAlbum {
            artists: vec![],
            album_type: "".to_string(),
            name: "black holes and revelations".to_string(),
            _type: Type::Artist,
            id: "black_holes_123".to_string(),
            external_urls: [
                ("spotify", "https://some_url".to_string())
            ].iter().collect(),
            available_markets: vec![],
            href: "".to_string(),
            images: vec![],
            uri: "".to_string()
        };

In the end, I would like some flexibility to create a simplified version of this struct with less code. I think I will try to work in this change this weekend, but just in case if someone also wants to do it...

Originally created by @mrcosta on GitHub (Feb 13, 2019). Original GitHub issue: https://github.com/ramsayleung/rspotify/issues/33 Hello, I'm unit testing my code and in order to do it I need to create a simplified version of `SimplifiedAlbum` (I understand the irony here of simplified version of something already simplified ) since I need just `external_urls`, `id`, `name`. An example of one that I'm creating: ```rust SimplifiedAlbum { artists: vec![], album_type: "".to_string(), name: "black holes and revelations".to_string(), _type: Type::Artist, id: "black_holes_123".to_string(), external_urls: [ ("spotify", "https://some_url".to_string()) ].iter().collect(), available_markets: vec![], href: "".to_string(), images: vec![], uri: "".to_string() }; ``` In the end, I would like some flexibility to create a simplified version of this struct with less code. I think I will try to work in this change this weekend, but just in case if someone also wants to do it...
kerem closed this issue 2026-02-27 20:22:36 +03:00
Author
Owner

@ramsayleung commented on GitHub (Feb 14, 2019):

It sounds great :)

<!-- gh-comment-id:463521002 --> @ramsayleung commented on GitHub (Feb 14, 2019): It sounds great :)
Author
Owner

@marioortizmanero commented on GitHub (Feb 25, 2021):

I'm all in for Default implementations. The example you provided can be replaced by:

SimplifiedAlbum {
    name: "black holes and revelations".to_string(),
    _type: Type::Artist,
    id: "black_holes_123".to_string(),
    external_urls: [
        ("spotify", "https://some_url".to_string())
    ].iter().collect(),
    ..Default::default()
};

It's a not-so-known trick that can come in very handy. Al we would need to do is add #[derive(Default)] to the models, and review non-trivial cases manually. A builder pattern for every model could be quite an overhead and a pain to maintain.

<!-- gh-comment-id:786280869 --> @marioortizmanero commented on GitHub (Feb 25, 2021): I'm all in for `Default` implementations. The example you provided can be replaced by: ```rust SimplifiedAlbum { name: "black holes and revelations".to_string(), _type: Type::Artist, id: "black_holes_123".to_string(), external_urls: [ ("spotify", "https://some_url".to_string()) ].iter().collect(), ..Default::default() }; ``` It's a not-so-known trick that can come in very handy. Al we would need to do is add `#[derive(Default)]` to the models, and review non-trivial cases manually. A builder pattern for every model could be quite an overhead and a pain to maintain.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/rspotify#11
No description provided.