Emulate Java Enums With Values in C# (Pt. 2, Improved With Conversion Operator Overload)

In a previous blog post, I demonstrated how Java’ enums that contain one or more values/objects can be emulated with C#. One thing bothered me, though: the switch statement and how inconvenient it was to determine the proper type. Worst of all, it was not type-safe. In my simple example, it was easy because I was using strings. Imagine your fake-enum does not contain a string to quickly identify the instance.

Well, there is a prettier workaround – and it involves an actual enum. I was thinking about how the same could be done in C++ and in C++, you can have type conversion operators. Then I searched if such a feature also exists in C#, and sure enough, it does.

Read More »

Emulate Java Enums With Values in C#

Update August 28, 2021

I have written a follow-up that improves on the following solution using a type conversion operator overload.

When I started dabbling in C#, I wondered if it supports values in enums. In Java, an enum instance can have properties (called fields in Java lingo) associated with the enum’s literals. By taking advantage of this feature, you can encode more information in an enum, like a string, for example, or a constant number. You can even embed instantiated class objects, maybe to associate an object factory with a literal.

In my use case, I wanted to achieve a form of a key-value-pair mapping. I require certain illegal characters in the NTFS file or directory names to be replaced with a given code. I use HTML encoding for my needs because I can simply look up the values online if I need to.

Here is the Java reference example. First, let me start with the basic enum definition (I use Lombok to auto-generate boilerplate code like the constructor and accessors).

@Getter
@RequiredArgsConstructor
enum CharacterReplacementCode  {
    COLON(":", "&58;"),
    POUND("#", "&35;"),
    QUESTION_MARK("?", "&63;"); 

    private final String character;
    private final String replacement;
    
    @Override
    public String toString() {
        return String.format("Character '%s' substituted by code '%s'", character, replacement); 
    }
}
Read More »

KDE Plasma Remap Meta/Windows Key From App Launcher to KRunner

Sometimes I want to run applications that I do not have pinned to the quick-launch bar of my choice’s operating system/desktop environment. To do that, I am used to pressing the Windows/Meta Key, begin typing a few characters, and hit Enter. This is muscle memory and hard to get rid of. Although it does not matter which UI opens, I do not need the full-blown KDE Application Launcher, Gnome Shell, or Windows Start Menu. The amount of UI that pops up and changes while searching for the app is distracting.

Therefore, I wondered whether I could remap the Meta/Windows key from opening the Application Launcher to opening KRunner. And you can, but only on the command line.

Remove the key mapping from the Application Launcher.

kwriteconfig5 --file kwinrc --group ModifierOnlyShortcuts --key Meta ""

Open KRunner instead.

kwriteconfig5 --file kwinrc --group ModifierOnlyShortcuts --key Meta "org.kde.krunner,/App,,toggleDisplay"

Apply the changes to the current session.

qdbus org.kde.KWin /KWin reconfigure

I hope this helps you. Thank you for reading.

Right to Repair: Do Not Forget Firmware

Early July 2021, US president Joe Biden signed an executive order strengthening the right to repair in America. It is all the rage in the YouTuber space. Over here in Europe, the European Parliament is also working on encouraging reuse and repair to save on resources (Ecodesign Requirements, Grant EU Consumers Right to Repair, Europe Reduce Waste by Guaranteeing Right to Repair). However, I do not think the movement is as strong as in the US, based on my perception of the media coverage. I had to actively search for information rather than having it thrown at me by media outlets, old-school and modern alike.

Disclaimer: This might just be my way of looking for and consuming information. I strongly prefer non-German modern tech media (read YouTube creators) because I am yet to find one that produces at the same level of production quality as someone like Linus Media Group, as one example. I watch German news, though, so I am not entirely ignoring my own country 😉

Now, the topic of this post is not where I get my information or how far the current state of legislation has come everywhere in the world.

I know that "Right to Repair" goes way beyond smartphones and computers. My focus is on consumer technology because that is where my interests are.

I want to talk about the software that runs on the hardware since it is just as important to a product’s lifetime. Washing machines and similar household appliances are becoming "smarter and smarter" with every new generation, so it is no longer just phones and tablets. Together with mobile computers, the latter two categories are likely what everybody interested in tech immediately thinks about when hearing "Right to Repair".

Read More »

Terraform Azure Error SoftDeletedVaultDoesNotExist

I just ran into a frustrating error that seemed unexplainable to me. My goal was to replace an existing Azure Resource Group with a new one managed entirely with Terraform. Besides a few other errors, this SoftDeletedVaultDoesNotExist was incredibly confusing because no more Key Vaults were found in the Resource Group’s list of resources.

Error: creating Vault: (Name "my-fancy-key-vault" / Resource Group "The-Codeslinger"): 
keyvault.VaultsClient#CreateOrUpdate: Failure sending request: StatusCode=0 -- 
Original Error: Code="SoftDeletedVaultDoesNotExist" 
Message="A soft deleted vault with the given name does not exist. 
Ensure that the name for the vault that is being attempted to recover is in a recoverable state. 
For more information on soft delete please follow this link https://go.microsoft.com/fwlink/?linkid=2149745"

with module.base.azurerm_key_vault.keyvault,
on terraform\key_vault.tf line 9, in resource "azurerm_key_vault" "keyvault":
    9: resource "azurerm_key_vault" "keyvault" {

That is because it was soft-delete enabled. And it was the Key Vault from the other Resource Group that I previously cleared of all resources, not the new Resource Group.

Using the az CLI you can display it, though.

> az keyvault list-deleted
[
    {
        "id": "/subscriptions/<subscription-id>/providers/Microsoft.KeyVault/locations/westeurope/deletedVaults/my-fancy-key-vault",
        "name": "my-fancy-key-vault",
        "properties": {
            "deletionDate": "2021-08-02T09:39:29+00:00",
            "location": "westeurope",
            "purgeProtectionEnabled": null,
            "scheduledPurgeDate": "2021-10-31T09:39:29+00:00",
            "tags": {
                "customer": "The-Codeslinger",
                "source": "Terraform"
            },
            "vaultId": "/subscriptions/<subscription-id>/resourceGroups/My-Other-ResourceGroup/providers/Microsoft.KeyVault/vaults/my-fancy-key-vault"
        },
        "type": "Microsoft.KeyVault/deletedVaults"
    }
]

And finally delete it.

> az keyvault purge --name my-fancy-key-vault

After that, it is gone.

$ az keyvault list-deleted
[]

Another option seems to be the Azure Portal, but I discovered this only after removing it on the command line.