Skip to content

Cli tool for generating a c# / f# / vb / cil enum based on a json input file.

License

Notifications You must be signed in to change notification settings

BastianBlokland/enum-generator-dotnet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

96 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EnumGenerator-Dotnet

Build Tests codecov License: MIT

Cli tool Global tool Core library
NuGet NuGet NuGet

Dotnet cli tool for generating c# / f# / vb / cil enums from json files.

Description

If you have config in json files it can be nice to have a enum to reference in the code instead of having to hard code values, this tool allows you to generate that enum.

Usage

There are 4 different ways to use the generator:

Usecase Project Documentation
Build integration Cli Cli Readme
Command line GlobalTool GlobalTool Readme
Manual library integration Core Core Readme
Unity3D package UnityPackage UnityPackage Readme

Json file structure

To be able to handle many different file structures the generator takes in a number of JsonPath entries:

Argument Usage
collection Path to the main collection in the json
entryname Path to the string name of a single entry in the above collection
entryvalue Path to the number value of a single entry in the above collection
entrycomment Path to the string comment of a single entry in the above collection

Note: If no entryvalue is provided the index in the collection will be used as the value.

Here's a couple structure examples:

  • Outer array

    json:

    [
      {
        "name": "A",
        "value": 1
      },
      {
        "name": "B",
        "value": 2
      },
    ]

    options:

    collection = "[*]"
    entryname = "name"
    entryvalue = "value"
  • Inner array

    json:

    {
      "entries": [
          "A",
          "B"
      ]
    }

    options:

    collection = "entries[*]"
    entryname = "$"
  • Inner object

    json:

    {
      "entries": [
        {
          "info": {
            "name": "A"
          },
          "value": 10
        },
        {
          "info": {
            "name": "B"
          },
          "value": 20
        }
      ]
    }

    options:

    collection = "entries[*]"
    entryname = "info.name"
    entryvalue = "value"
  • Deep search

    json:

    {
      "collection1": {
        "entries": [
          {
            "name": "A"
          },
          {
            "name": "B"
          }
        ]
      },
      "collection2": {
        "entries": [
          {
            "name": "C"
          },
          {
            "name": "D"
          }
        ]
      }
    }

    options:

    collection = "..entries[*]"
    entryname = "name"
    entryvalue = "value"
  • Filtering

    json:

    [
      {
        "name": "A",
        "value": 1,
        "active": false
      },
      {
        "name": "B",
        "value": 2,
        "active": true
      },
      {
        "name": "C",
        "value": 3,
        "active": true
      },
      {
        "name": "D",
        "value": 4,
        "active": false
      }
    ]

    options:

    collection = "[?(@.active == true)]"
    entryname = "name"
    entryvalue = "value"

Note: Enable verbose logging to get more output about what the mapper is doing.

Integration example

An example can be found in the example directory.