Skip to content

Commit

Permalink
tools, northbound: improve reserved keyword handling in code generation
Browse files Browse the repository at this point in the history
Use an external crate to handle all possible reserved keywords
(including "as", present in the BGP YANG model).

Signed-off-by: Renato Westphal <[email protected]>
  • Loading branch information
rwestphal committed Nov 20, 2023
1 parent 609f4c7 commit 0431911
Show file tree
Hide file tree
Showing 5 changed files with 7 additions and 7 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ async-trait = "0.1"
bitflags = { version = "2.4", features = ["serde"] }
bytes = { version = "1", features = ["serde"] }
capctl = "0.2"
check_keyword = "0.2"
clap = "2.33"
chrono = { version = "0.4", features = ["serde"] }
criterion = "0.4"
Expand Down
1 change: 1 addition & 0 deletions holo-northbound/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ holo-yang = { path = "../holo-yang" }
holo-utils = { path = "../holo-utils" }

[build-dependencies]
check_keyword.workspace = true
yang2.workspace = true
holo-yang = { path = "../holo-yang" }
5 changes: 2 additions & 3 deletions holo-northbound/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::env;
use std::fmt::Write;
use std::path::PathBuf;

use check_keyword::CheckKeyword;
use holo_yang as yang;
use holo_yang::YANG_IMPLEMENTED_MODULES;
use yang2::schema::{DataValue, SchemaNode, SchemaNodeKind, SchemaPathFormat};
Expand All @@ -19,9 +20,7 @@ fn snode_normalized_name(snode: &SchemaNode<'_>) -> String {
name = str::replace(&name, "-", "_");

// Handle Rust reserved keywords.
if name == "type" {
name = "r#type".to_owned();
}
name = name.into_safe();

// HACK: distinguish nodes with the same names but different namespaces.
if matches!(
Expand Down
1 change: 1 addition & 0 deletions holo-tools/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ license.workspace = true
edition.workspace = true

[dependencies]
check_keyword.workspace = true
clap.workspace = true
num-traits.workspace = true
serde_json.workspace = true
Expand Down
6 changes: 2 additions & 4 deletions holo-tools/src/yang_callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use std::fmt::Write;

use check_keyword::CheckKeyword;
use clap::{App, Arg};
use holo_northbound::CallbackOp;
use holo_yang as yang;
Expand All @@ -23,10 +24,7 @@ fn snode_module_path(snode: &SchemaNode<'_>) -> String {
// Replace hyphens by underscores.
name = str::replace(&name, "-", "_");
// Handle Rust reserved keywords.
if name == "type" {
name = "r#type".to_owned();
}
name
name.into_safe()
})
.collect::<Vec<String>>()
.join("::");
Expand Down

0 comments on commit 0431911

Please sign in to comment.