Skip to content

Commit

Permalink
Adds --derive CLI argument for deriving additional traits on genera…
Browse files Browse the repository at this point in the history
…ted structs/enums (#20)

* w/e

* Update tests and config to allow for optional --derives command-line argument.

* Comment out tests which fail to compile.

* Change wording.

* Remove .vscode for PR

* Undo Cargo.toml change.

* Remove unused method.

* Change more wording.
  • Loading branch information
tbrockman authored Mar 24, 2024
1 parent 363882a commit 9e45e9f
Show file tree
Hide file tree
Showing 11 changed files with 596 additions and 299 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,34 @@ Use the command line help to see required arguments & options when generating li

The open source version builds client libraries for Rust. Libninja also supports other languages with a commercial license. Reach out at the email in author Github profile.

# Advanced usage

# Usage
## Deriving traits for generated structs

## Customizing generation
You can derive traits for the generated structs by passing them using one (or many) `--derive` arguments:

```bash
libninja gen --lang rust --repo libninjacom/plaid-rs --derive oasgen::OaSchema -o . Plaid ~/path/to/plaid/openapi.yaml
```

Make sure to add the referenced crate(s) (and any necessary features) to your `Cargo.toml`:

```bash
cargo add oasgen --features chrono
```

Then, the traits will be added to the `derive` attribute on the generated `model` and `request` structs:
```rust
use serde::{Serialize, Deserialize};
use super::Glossary;
#[derive(Debug, Clone, Serialize, Deserialize, Default, oasgen::OaSchema)]
pub struct ListGlossariesResponse {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub glossaries: Option<Vec<Glossary>>,
}
```

## Customizing generation further

There are two ways to customize codegen, first by modifying the OpenAPI spec, and second, using a file template system.

Expand All @@ -53,6 +77,6 @@ Alternatively, if the string `libninja: static` is found in the file template, i

# Development

If you run into errors about a missing `commericial` package, run the command `just dummy_commericial` to create a dummy
If you run into errors about a missing `commericial` package, run the command `just dummy_commercial` to create a dummy
package.

13 changes: 9 additions & 4 deletions core/src/options.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use std::path::PathBuf;
use convert_case::{Case, Casing};
use mir::{literal, Literal};
use hir::Language;

use mir::{literal, Literal};
use proc_macro2::TokenStream;
use quote::quote;
use std::path::PathBuf;

#[derive(Debug, Clone, Default)]
pub struct ConfigFlags {
/// Only for Rust. Adds ormlite::TableMeta flags to the code.
pub ormlite: bool,
/// Only for Rust (for now). Adds fake::Dummy flags to the code.
pub fake: bool
pub fake: bool,
}

#[derive(Debug, Clone)]
Expand All @@ -26,6 +27,8 @@ pub struct PackageConfig {
pub config: ConfigFlags,

pub dest: PathBuf,

pub derives: Vec<String>,
}

impl PackageConfig {
Expand Down Expand Up @@ -79,4 +82,6 @@ pub struct OutputConfig {
pub github_repo: Option<String>,

pub version: Option<String>,

pub derive: Vec<String>,
}
40 changes: 25 additions & 15 deletions libninja/src/command/generate.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::path::{Path, PathBuf};
use std::process::Output;
use crate::{generate_library, read_spec, Language, OutputConfig, PackageConfig};
use anyhow::Result;
use clap::{Args, ValueEnum};
use convert_case::{Case, Casing};
use ln_core::ConfigFlags;
use std::path::{Path, PathBuf};
use std::process::Output;
use tracing::debug;
use crate::{OutputConfig, Language, PackageConfig, read_spec, generate_library};
use ln_core::{ConfigFlags};

#[derive(ValueEnum, Debug, Clone, Copy)]
pub enum Config {
Expand Down Expand Up @@ -60,24 +60,34 @@ pub struct Generate {

/// Path to the OpenAPI spec file.
spec_filepath: String,

/// List of additional namespaced traits to derive on generated structs.
#[clap(long)]
derive: Vec<String>,
}

impl Generate {
pub fn run(self) -> Result<()> {
let package_name = self.package_name.unwrap_or_else(|| self.name.to_lowercase());
let package_name = self
.package_name
.unwrap_or_else(|| self.name.to_lowercase());

let path = PathBuf::from(self.spec_filepath);
let output_dir = self.output_dir.unwrap_or_else(|| ".".to_string());
let spec = read_spec(&path)?;
generate_library(spec, OutputConfig {
dest_path: PathBuf::from(output_dir),
config: build_config(&self.config),
language: self.language,
build_examples: self.examples.unwrap_or(true),
package_name,
service_name: self.name.to_case(Case::Pascal),
github_repo: self.repo,
version: self.version,
})
generate_library(
spec,
OutputConfig {
dest_path: PathBuf::from(output_dir),
config: build_config(&self.config),
language: self.language,
build_examples: self.examples.unwrap_or(true),
package_name,
service_name: self.name.to_case(Case::Pascal),
github_repo: self.repo,
version: self.version,
derive: self.derive,
},
)
}
}
Loading

0 comments on commit 9e45e9f

Please sign in to comment.