Skip to content

Commit 39fb18b

Browse files
(GH-538) Idiomaticize schemas for enums in dsc-lib
This change uses the new `idiomaticize_*` transformers from the `dsc-lib-jsonschema` crate in the `dsc-lib` crate to ensure that the generated schemas are idiomatic.
1 parent 12f6f91 commit 39fb18b

File tree

12 files changed

+189
-1
lines changed

12 files changed

+189
-1
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/dsc-lib/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ which = { workspace = true }
4343
# workspace crate dependencies
4444
dsc-lib-osinfo = { workspace = true }
4545
dsc-lib-security_context = { workspace = true }
46+
dsc-lib-jsonschema = { workspace = true }
4647
tree-sitter-dscexpression = { workspace = true }
4748

4849
[dev-dependencies]

lib/dsc-lib/src/configure/config_doc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// Licensed under the MIT License.
33

44
use chrono::{DateTime, Local};
5+
use dsc_lib_jsonschema::transforms::{
6+
idiomaticize_externally_tagged_enum,
7+
idiomaticize_string_enum
8+
};
59
use rust_i18n::t;
610
use schemars::{JsonSchema, json_schema};
711
use serde::{Deserialize, Serialize};
@@ -12,6 +16,7 @@ use crate::{dscerror::DscError, schemas::DscRepoSchema};
1216

1317
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
1418
#[serde(rename_all = "camelCase")]
19+
#[schemars(transform = idiomaticize_string_enum)]
1520
pub enum SecurityContextKind {
1621
Current,
1722
Elevated,
@@ -20,6 +25,7 @@ pub enum SecurityContextKind {
2025

2126
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
2227
#[serde(rename_all = "camelCase")]
28+
#[schemars(transform = idiomaticize_string_enum)]
2329
pub enum Operation {
2430
Get,
2531
Set,
@@ -29,6 +35,7 @@ pub enum Operation {
2935

3036
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
3137
#[serde(rename_all = "camelCase")]
38+
#[schemars(transform = idiomaticize_string_enum)]
3239
pub enum ExecutionKind {
3340
Actual,
3441
WhatIf,
@@ -43,6 +50,7 @@ pub struct Process {
4350

4451
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
4552
#[serde(rename_all = "camelCase")]
53+
#[schemars(transform = idiomaticize_externally_tagged_enum)]
4654
pub enum RestartRequired {
4755
System(String),
4856
Service(String),
@@ -190,6 +198,7 @@ pub struct Parameter {
190198
}
191199

192200
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
201+
#[schemars(transform = idiomaticize_string_enum)]
193202
pub enum DataType {
194203
#[serde(rename = "string")]
195204
String,
@@ -223,6 +232,7 @@ impl Display for DataType {
223232
}
224233

225234
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
235+
#[schemars(transform = idiomaticize_string_enum)]
226236
pub enum CopyMode {
227237
#[serde(rename = "serial")]
228238
Serial,

lib/dsc-lib/src/configure/config_progress.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
3+
use dsc_lib_jsonschema::transforms::idiomaticize_string_enum;
44
use schemars::JsonSchema;
55
use serde::{Deserialize, Serialize};
66

@@ -13,6 +13,7 @@ pub struct ConfigurationResourceStartedEvent {
1313

1414
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
1515
#[serde(rename_all = "camelCase")]
16+
#[schemars(transform = idiomaticize_string_enum)]
1617
pub enum ConfigurationResourceCompletionStatus {
1718
Success,
1819
Failure,

lib/dsc-lib/src/configure/config_result.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
use dsc_lib_jsonschema::transforms::idiomaticize_string_enum;
45
use schemars::JsonSchema;
56
use serde::{Deserialize, Serialize};
67
use serde_json::{Map, Value};
@@ -9,6 +10,7 @@ use crate::configure::config_doc::{Configuration, Metadata};
910

1011
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
1112
#[serde(rename_all = "camelCase")]
13+
#[schemars(transform = idiomaticize_string_enum)]
1214
pub enum MessageLevel {
1315
Error,
1416
Warning,

lib/dsc-lib/src/discovery/command_discovery.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::extensions::dscextension::{self, DscExtension, Capability as Extensio
1313
use crate::extensions::extension_manifest::ExtensionManifest;
1414
use crate::progress::{ProgressBar, ProgressFormat};
1515
use crate::util::convert_wildcard_to_regex;
16+
use dsc_lib_jsonschema::transforms::idiomaticize_externally_tagged_enum;
1617
use regex::RegexBuilder;
1718
use rust_i18n::t;
1819
use semver::{Version, VersionReq};
@@ -40,6 +41,7 @@ static EXTENSIONS: LazyLock<RwLock<BTreeMap<String, DscExtension>>> = LazyLock::
4041
static ADAPTED_RESOURCES: LazyLock<RwLock<BTreeMap<String, Vec<DscResource>>>> = LazyLock::new(|| RwLock::new(BTreeMap::new()));
4142

4243
#[derive(Clone, Serialize, Deserialize, JsonSchema)]
44+
#[schemars(transform = idiomaticize_externally_tagged_enum)]
4345
pub enum ImportedManifest {
4446
Resource(DscResource),
4547
Extension(DscExtension),

lib/dsc-lib/src/dscresources/dscresource.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use crate::{configure::{Configurator, config_doc::{Configuration, ExecutionKind, Resource}, context::ProcessMode, parameters::{SECURE_VALUE_REDACTED, is_secure_value}}, dscresources::resource_manifest::{AdapterInputKind, Kind}};
55
use crate::dscresources::invoke_result::{ResourceGetResponse, ResourceSetResponse};
6+
use dsc_lib_jsonschema::transforms::idiomaticize_string_enum;
67
use dscerror::DscError;
78
use jsonschema::Validator;
89
use rust_i18n::t;
@@ -59,6 +60,7 @@ pub struct DscResource {
5960

6061
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
6162
#[serde(rename_all = "camelCase")]
63+
#[schemars(transform = idiomaticize_string_enum)]
6264
pub enum Capability {
6365
/// The resource supports retrieving configuration.
6466
Get,

lib/dsc-lib/src/dscresources/resource_manifest.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
use dsc_lib_jsonschema::transforms::idiomaticize_string_enum;
45
use rust_i18n::t;
56
use schemars::{Schema, JsonSchema, json_schema};
67
use semver::Version;
@@ -12,6 +13,7 @@ use crate::{dscerror::DscError, schemas::DscRepoSchema};
1213

1314
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
1415
#[serde(rename_all = "camelCase")]
16+
#[schemars(transform = idiomaticize_string_enum)]
1517
pub enum Kind {
1618
Adapter,
1719
Exporter,
@@ -94,6 +96,7 @@ pub enum ArgKind {
9496
}
9597

9698
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
99+
#[schemars(transform = idiomaticize_string_enum)]
97100
pub enum InputKind {
98101
/// The input is accepted as environmental variables.
99102
#[serde(rename = "env")]
@@ -122,6 +125,7 @@ pub struct SchemaCommand {
122125
}
123126

124127
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
128+
#[schemars(transform = idiomaticize_string_enum)]
125129
pub enum ReturnKind {
126130
/// The return JSON is the state of the resource.
127131
#[serde(rename = "state")]
@@ -224,6 +228,7 @@ pub struct Adapter {
224228
}
225229

226230
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize, JsonSchema)]
231+
#[schemars(transform = idiomaticize_string_enum)]
227232
pub enum AdapterInputKind {
228233
/// The adapter accepts full unprocessed configuration.
229234
#[serde(rename = "full")]

lib/dsc-lib/src/extensions/dscextension.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
33

4+
use dsc_lib_jsonschema::transforms::idiomaticize_string_enum;
45
use serde::{Deserialize, Serialize};
56
use serde_json::Value;
67
use schemars::JsonSchema;
@@ -33,6 +34,7 @@ pub struct DscExtension {
3334

3435
#[derive(Clone, Debug, PartialEq, Deserialize, Serialize, JsonSchema)]
3536
#[serde(rename_all = "camelCase")]
37+
#[schemars(transform = idiomaticize_string_enum)]
3638
pub enum Capability {
3739
/// The extension aids in discovering resources.
3840
Discover,
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
//! Defines integration tests for [`dsc-lib`].
5+
//!
6+
//! Instead of defining tests in each of the module files for the crate, we define them here as
7+
//! integration tests to improve compilation times.
8+
//!
9+
//! The tests in this module are for public code. The tests should validate expected behaviors at
10+
//! the public API level. Don't add tests to this module for inner code behaviors.
11+
//!
12+
//! We organize the tests in the `tests/integration` folder instead of directly in `tests` to
13+
//! minimize compilation times. If we defined the tests one level higher in the `tests` folder,
14+
//! Rust would generate numerous binaries to execute our tests.
15+
16+
#[cfg(test)] mod schemas;

0 commit comments

Comments
 (0)