Skip to content

Commit bd6079e

Browse files
feat(stackable-versioned): Add support for modules (#891)
* chore: Add changelog entry * chore: Update PR link in changelog * refactor: Modularize token generation This separates token generation code to be more modular. Depending on the context, individual parts of the code need to be placed in different locations. Two major refactors are the change of macro input as well as moving module generation out of the struct definition generation. This commit doesn't include changes for enum generation. This will be done in a separate commit. * refactor: Use fully qualified From path * refactor: Adjust enum generation code * feat: Add support for modules This commit contains a major rework of most parts of the attribute macro to support attaching it to modules. Most of the previously existing features are already in place. There are only two missing features which will be added in follow-up commits: version specific doc comments and merged CRD YAML support. Because of that, some snapshot tests are failing. * fix: Correctly emit automatically_derived and deprecated attributes * fix: Skip From impls for enum if options are set * fix: Correctly emit doc commens * fix: Correctly emit merged_crd code * fix: Correctly skip merged_crd generation This also adds a test to validate that the relevant code is not generated of the skip option is set. * chore: Fix clippy errors * test: Add module snapshot tests * test: Update basic Kubernetes snapshot test * doc: Fix doc comment links * chore: Remove unused strum dependency * ci(pre-commit): Update rust version and use stackabletech/actions/run-pre-commit * fix: Correctly report missing Spec suffix * test: Update compile-fail test cases * chore: Update changelog * ci(pre-commit): Add rust-src component * ci(build): Bump Rust version * chore: Apply suggestions Co-authored-by: Nick <10092581+NickLarsenNZ@users.noreply.github.com> * chore(test): Add comment about empty versioned attribute * chore: Add missing doc comments for supported K8s arguments * chore: Replace unwrap with expect * chore: Adjust FIXME comment * chore: Add explanation comment * chore: Remove outdated comments * chore: Add doc comment to validate_added_action function * chore: Replace mustn't with must not in error message * chore: Use concrete type instead of generic type in BTreeMapExt impl * chore: Add doc comments --------- Co-authored-by: Nick <10092581+NickLarsenNZ@users.noreply.github.com>
1 parent de15662 commit bd6079e

File tree

54 files changed

+2856
-2605
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+2856
-2605
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ env:
1717
CARGO_TERM_COLOR: always
1818
CARGO_INCREMENTAL: '0'
1919
CARGO_PROFILE_DEV_DEBUG: '0'
20-
RUST_TOOLCHAIN_VERSION: "1.81.0"
20+
RUST_TOOLCHAIN_VERSION: "1.82.0"
2121
RUSTFLAGS: "-D warnings"
2222
RUSTDOCFLAGS: "-D warnings"
2323
RUST_LOG: "info"

.github/workflows/pr_pre-commit.yaml

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ on:
66

77
env:
88
CARGO_TERM_COLOR: always
9-
RUST_TOOLCHAIN_VERSION: "1.81.0"
9+
RUST_TOOLCHAIN_VERSION: "1.82.0"
1010
HADOLINT_VERSION: "v1.17.6"
1111

1212
jobs:
@@ -16,29 +16,10 @@ jobs:
1616
- uses: actions/checkout@eef61447b9ff4aafe5dcd4e0bbf5d482be7e7871 # v4.2.1
1717
with:
1818
fetch-depth: 0
19-
- uses: actions/setup-python@82c7e631bb3cdc910f68e0081d67478d79c6982d # v5.1.0
19+
- uses: stackabletech/actions/run-pre-commit@9bd13255f286e4b7a654617268abe1b2f37c3e0a # v0.3.0
2020
with:
21-
python-version: '3.12'
22-
- uses: dtolnay/rust-toolchain@master
23-
with:
24-
toolchain: ${{ env.RUST_TOOLCHAIN_VERSION }}
25-
components: rustfmt,clippy
26-
- name: Setup Hadolint
27-
shell: bash
28-
run: |
29-
set -euo pipefail
30-
31-
LOCATION_DIR="$HOME/.local/bin"
32-
LOCATION_BIN="$LOCATION_DIR/hadolint"
33-
34-
SYSTEM=$(uname -s)
35-
ARCH=$(uname -m)
36-
37-
mkdir -p "$LOCATION_DIR"
38-
curl -sL -o "${LOCATION_BIN}" "https://github.com/hadolint/hadolint/releases/download/${{ env.HADOLINT_VERSION }}/hadolint-$SYSTEM-$ARCH"
39-
chmod 700 "${LOCATION_BIN}"
40-
41-
echo "$LOCATION_DIR" >> "$GITHUB_PATH"
42-
- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
43-
with:
44-
extra_args: "--from-ref ${{ github.event.pull_request.base.sha }} --to-ref ${{ github.event.pull_request.head.sha }}"
21+
rust: ${{ env.RUST_TOOLCHAIN_VERSION }}
22+
# rust-src is required for trybuild stderr output comparison to work
23+
# for our cases.
24+
# See: https://github.com/dtolnay/trybuild/issues/236#issuecomment-1620950759
25+
rust-components: rustfmt,clippy,rust-src

Cargo.lock

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

crates/stackable-versioned-macros/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ itertools.workspace = true
3838
k8s-openapi = { workspace = true, optional = true }
3939
kube = { workspace = true, optional = true }
4040
proc-macro2.workspace = true
41-
strum.workspace = true
4241
syn.workspace = true
4342
quote.workspace = true
4443

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1"),
4+
version(name = "v2alpha1")
5+
)]
6+
// ---
7+
pub(crate) mod versioned {
8+
pub struct Foo {
9+
bar: usize,
10+
11+
#[versioned(added(since = "v1"))]
12+
baz: bool,
13+
14+
#[versioned(deprecated(since = "v2alpha1"))]
15+
deprecated_foo: String,
16+
}
17+
18+
// The following attribute is just to ensure no strange behavior occurs.
19+
#[versioned]
20+
pub struct Bar {
21+
baz: String,
22+
}
23+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1"),
4+
version(name = "v2alpha1"),
5+
preserve_module
6+
)]
7+
// ---
8+
pub(crate) mod versioned {
9+
pub struct Foo {
10+
bar: usize,
11+
12+
#[versioned(added(since = "v1"))]
13+
baz: bool,
14+
15+
#[versioned(deprecated(since = "v2alpha1"))]
16+
deprecated_foo: String,
17+
}
18+
19+
#[versioned]
20+
pub struct Bar {
21+
baz: String,
22+
}
23+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#[versioned(
2+
version(name = "v1alpha1"),
3+
version(name = "v1beta1"),
4+
version(name = "v1"),
5+
k8s(
6+
group = "stackable.tech",
7+
singular = "foo",
8+
plural = "foos",
9+
namespaced,
10+
skip(merged_crd)
11+
)
12+
)]
13+
// ---
14+
#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
15+
pub struct FooSpec {
16+
#[versioned(
17+
added(since = "v1beta1"),
18+
changed(since = "v1", from_name = "bah", from_type = "u16")
19+
)]
20+
bar: usize,
21+
baz: bool,
22+
}

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@attribute_enum.rs.snap

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

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@attribute_struct.rs.snap

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

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@basic_struct.rs.snap

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

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@deprecate_enum.rs.snap

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

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@deprecate_struct.rs.snap

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

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@enum_data_simple.rs.snap

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

crates/stackable-versioned-macros/fixtures/snapshots/stackable_versioned_macros__test__default_snapshots@module.rs.snap

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

0 commit comments

Comments
 (0)