Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

datastore: allow simple enums as map keys #2687

Merged
merged 1 commit into from
Dec 29, 2022

Conversation

webern
Copy link
Member

@webern webern commented Dec 29, 2022

Issue number:

Closes #2686

Description of changes:

This allows us to use an enum as a map key in the model, which is something we want to do in the upcoming implementation for #1703

Testing done:

Created this enum

#[derive(Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize, Scalar)]
#[serde(rename_all = "snake_case")]
pub enum RandomWords {
    Gloom,
    TheftClash,
}

Added it to the EcsSettings struct

// ECS settings.
#[model]
struct ECSSettings {
    /* ... */
    stuff: HashMap<RandomWords, String>,
}

Ran an aws-ecs-1 variant:

Tried to set a bad map key, api rejected it as expected:

apiclient set ecs.stuff.bad_key=value
Unable to match your input to the data model.  We may not have enough type information.  Please try the --json input form.  Cause: Error during deserialization: unknown variant `bad_key`, expected one of `begin`, `bet`, `crack`, `elect`, `fix`, `gloom`, `item`, `lump`, `new`, `news`, `strap`, `theft_clash`, `time`, `tip`

Set a couple of good key/vals:

apiclient set ecs.stuff.theft_clash=value_1
apiclient set ecs.stuff.gloom=value_2

Get the map:

apiclient get settings.ecs.stuff
{
  "settings": {
    "ecs": {
      "stuff": {
        "gloom": "value_2",
        "theft_clash": "value_1"
      }
    }
  }
}

Terms of contribution:

By submitting this pull request, I agree that this contribution is dual-licensed under the terms of both the Apache License, version 2.0, and the MIT license.

An enum that can be serialized as a simple, scalar string can now be
used as a map key in the datastore.
@webern webern requested review from yeazelm and jpmcb December 29, 2022 00:16
Copy link
Contributor

@yeazelm yeazelm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Contributor

@stmcginnis stmcginnis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@webern webern merged commit aeb95d8 into bottlerocket-os:develop Dec 29, 2022
@webern webern deleted the keys branch December 29, 2022 21:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

datastore: allow simple enums as map keys
3 participants