jsontypedef / json-typedef-codegen

A CLI tool that generates code from JSON Typedef schemas
https://jsontypedef.com/docs/tools/jtd-codegen
MIT License
160 stars 31 forks source link

Add support for `rustCustomDerive` and `rustCustomUse` metadata #31

Open AdamLeyshon opened 3 years ago

AdamLeyshon commented 3 years ago

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,
}
ucarion commented 3 years ago

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 commented 2 years ago

Any chance this gets merged?

AdamLeyshon commented 2 years ago

@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 commented 1 year ago

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

alissapajer commented 1 year ago

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