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

Add support for rustCustomDerive and rustCustomUse metadata #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

AdamLeyshon
Copy link

@AdamLeyshon AdamLeyshon commented Jul 12, 2021

This allows us to additional use statements or derive macros on generated structs as per my request in #30

rustCustomUse is a string of use statments separated by ;
It supports single and multi-import statements.
For example: serde::{Deserialize, Serialize} or serde::Serialize are both valid.

rustCustomDerive is a string of derive identifers separated by ,
For example: FromSqlRow,AsExpression,Debug,Default

JTD JSON Example:

{
  "metadata": {
    "rustCustomDerive": "FromSqlRow, AsExpression, Debug, Default",
    "rustCustomUse": "std::fmt::Debug;crate::lib::crypto::{hash_string, sign_string}"
  },
  "properties": {
    "inventory": {
      "metadata": {
        "rustCustomDerive": "Default,Debug"
      },
      "properties": {
        "threshold": {
          "type": "uint32"
        }
      }
    }
  }
}

Outputs the following:

// Code generated by jtd-codegen for Rust v0.2.1

use crate::lib::crypto::{hash_string, sign_string};
use serde::{Deserialize, Serialize};
use std::fmt::Debug;

#[derive(Serialize, Deserialize, Default, Debug)]
pub struct ApiConfigDataInventory {
    #[serde(rename = "threshold")]
    pub threshold: u32,
}

#[derive(Serialize, Deserialize, FromSqlRow,  AsExpression,  Debug,  Default)]
pub struct ApiConfigData {
    #[serde(rename = "inventory")]
    pub inventory: ApiConfigDataInventory,
}

…tructs.

This allows us to additional `use` statements or `derive` macros on generated structs.

`rustCustomUse` is a string of use statments separated by `;`
It supports single and multi-import statements.
For example: `serde::{Deserialize, Serialize}` or `serde::Serialize` are both valid.

`rustCustomUse` is a string of derive identifers separated by `,`
For example: `FromSqlRow,AsExpression,Debug,Default`
@ucarion
Copy link
Contributor

ucarion commented Jul 17, 2021

Hi, I just want to acknowledge that I've seen this PR, and that I'll try to get to it when time permits.

Thanks so much for taking the time to contribute!

@frenzox
Copy link

frenzox commented May 2, 2022

Any chance this gets merged?

@AdamLeyshon
Copy link
Author

@frenzox I raised this PR almost exactly a year ago, looks like the whole JTD org/projects are dead, there's been no activity in any of the repos for months except for Dependabot PRs.

@alissapajer
Copy link

This is exactly what we need! Merging this in to my fork. Thank you!

@alissapajer
Copy link

Rust custom derives for all generated types: https://github.com/spindle-app/json-typedef-codegen/pull/4/files

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.

4 participants