Giter Club home page Giter Club logo

smartenums's Issues

Add ability to Search by EnumValue (similar to search by tag)

I have this enum (see below) and I need a way to lookup the enum from the Code's value so I can use the enum to get the Cluster's value. It would be nice if this was a builtin feature like FindByTag.

I've found that I can do it like this:
return SmartEnum.GetEnumerator<EventType>().Single(x => x.GetValueOf<string>("Code") == code);
but it would be nice to have a simpler syntax that is an extension method (again like FindByTag).

public enum EventType
{
	[EnumValue("Code", "T")]
	[EnumValue("Cluster", 1)]
	Trade,

	[EnumValue("Code", "Q")]
	[EnumValue("Cluster", 1)]
	Quote,

	[EnumValue("Code", "A")]
	[EnumValue("Cluster", 2)]
	AggregatePerSecond,

	[EnumValue("Code", "AM")]
	[EnumValue("Cluster", 2)]
	AggregatePerMinute,

	[EnumValue("Code", "I")]
	[EnumValue("Cluster", 2)]
	Imbalance,

	[EnumValue("Code", "Status")]
	[EnumValue("Cluster", 0)]
	Status,
}

Improve error handling

Now versions in EnumValueAttribute can be null and sometimes it can throw null exception. Need to fix it.

Fix summary in nuget package

There are summary docs for main methods in project but when build source to package in new version has not summary.

SmartEnum.GetEnumerator FieldNotImplementException

Given these enums I would like to be able to do the following but I can't:
var eventTypesCluster2 = SmartEnum.GetEnumerator<EventType>().Where(x => x.GetValueOf<Cluster>("Cluster") == Cluster.Two).ToList();

The goal here as you can see is to get a subset of valid enum values for a given situation but if just one enum value does not have the EnumValue with the specified key the whole enumeration fails??? That makes no sense!

I can demonstrate that issue more clearly like this:
var test = EventType.Status.GetValueOf<Cluster>("Cluster");
My expectation would be that this should return null since the EnumValue doesn't exist.

but I could kinda understand your apprehension there since it doesn't really make sense for non-nullable types like enum Cluster in this case.

A possible solution would be to keep allowing GetValueOf<> to throw the FieldNotImplementException for non-nullable types but to return null for nullable types. With that solution by adding the ? now any variable can be nullable and would return null rather than the FieldNotImplementException:
var test = EventType.Status.GetValueOf<Cluster?>("Cluster");

If you're not so keen on my previous suggestion hopefully you'll like this potential solution because it should feel familiar. Make a TryGetValueOf<> method.
An example might look like this:
var test = EventType.Status.TryGetValueOf<Cluster>("Cluster", out var clusterValue);

In my opinion I would say you should strongly consider implementing both options because being able to enumerate the enum without throwing an exception should be valuable in many cases.

public enum Cluster
{
	Unknown = -1,
	Zero = 0,
	One = 1,
	Two = 2
}

public enum EventType
{
	[EnumValue("Code", "T")]
	[EnumValue("Cluster", Cluster.One)]
	Trade,

	[EnumValue("Code", "Q")]
	[EnumValue("Cluster", Cluster.One)]
	Quote,

	[EnumValue("Code", "A")]
	[EnumValue("Cluster", Cluster.Two)]
	AggregatePerSecond,

	[EnumValue("Code", "AM")]
	[EnumValue("Cluster", Cluster.Two)]
	AggregatePerMinute,

	[EnumValue("Code", "I")]
	[EnumValue("Cluster", Cluster.Two)]
	Imbalance,

	[EnumValue("Code", "Status")]
	Status,
}

SmartEnums 4.0 (generated enums)

  • Realize enum generator;
  • Realize adding enumValues to generated enums;
  • Realize adding tags to generated enums;
  • Realize work with extensions;
  • Realize work with SmartEnum class;
  • Realize tests;
  • Update documentation;

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.