Is there a quick way to look up an Enum with only using the label of the enum instead of the value. Let's say the Enum type is SalesStatus, I want to be able to basically call into some kind of a function like enumLabel2Value(enumStr(SalesStatus), "Open order") and it will return 1.
I'm trying to avoid looping thru all possible values and checking each one separately, it seems like this should be something that's readily available since whenever a user filters on a enum column on a grid, they enter in the label, not the value, but I haven't seen anything like it.
You can use the str2Enum function for that. From the documentation:
Retrieves the enum element whose localized Label property value
matches the input string.
In addition to the caveats from Alex Kwitny's answer, I recommend taking a look at the comments of the documentation, specifically the comment
Please note that str2Enum performs partial matching and matches the
beginning of the string. If there are multiple matches, it will take
the first one.
In addition take a look at method string2Enum of class DMFEntityBase, which supports different options how the enum element can be specified. I think with the DictEnum.name2Value() method enum elements specified by their label are handled.
Update
OP mentioned in the comments to Alex Kwitny's answer that it is a specific enum ExchangeRateDisplayFactor he has issues with. str2Enum also works with that enum, as the following job demonstrates:
static void str2EnumTest(Args _args)
{
ExchangeRateDisplayFactor factor;
factor = str2Enum(factor, '1');
info(strFmt('%1', factor)); // outputs '1'
factor = str2Enum(factor, '10');
info(strFmt('%1', factor)); // outputs '10'
factor = str2Enum(factor, '100');
info(strFmt('%1', factor)); // outputs '100'
factor = str2Enum(factor, '1000');
info(strFmt('%1', factor)); // outputs '1000'
factor = str2Enum(factor, '10000');
info(strFmt('%1', factor)); // outputs '10000'
}
It doesn't exist because labels can be all sorts of things in different languages. symbol2Value() exists though and may be what you're looking for, but your question is specifically on labels. An example of where this could be very bad...
Let's say you have an enum called GoodBadPresent, to indicate what type of Christmas present you will receive, with two values:
GoodBadPresent::Poison English label: "Poison"; German label: "Gift"
GoodBadPresent::Gift English label: "Gift"; German label: "Geschenk"
If this example isn't clear, the word for Poison in German is Gift. So if you tried to resolve Gift to an enum value, you'd also have to provide the language. The performance problems here are probably greater than the performance problems of looping through an enum.
You can look at DictEnum to see if there are any methods that can help you more succinctly achieve what you want though. https://msdn.microsoft.com/en-us/library/gg837824.aspx
I'm more curious to the details of your scenario where you need to get back to an enum from a label.
Related
The Code
Consider the following protobuf message declaration:
syntax = "proto3";
enum Airport {
TLV = 0;
JFK = 1;
...
}
message Flight {
...
Airport origin_airport = 11;
...
}
The Problem
Due to some business requirements, we have to set the Airport to be a free string rather than choosing from a closed enumerated list. I know that I can add and remove fields at will, as long as I don't reuse the same number. However, I'm not sure if I can use the same name, a-la:
message Flight {
...
reserved 11; // The old enum field number
string origin_airport = 18; // Unused so far
...
}
My Question
Upon updaing a protobuf3 field type, can the field name be preserved, as long as its number changes?
If you aren't using the JSON variant, then names aren't used at all in the payload, so yes technically it is perfectly legal to reuse names; however: this might lead to unnecessary problems with existing code - depending on existing code and language / framework specific rules, and could cause confusion. Since that is avoidable, I would advocate using a name like origin_airport_code, or similar.
(The point I'm making here: any code that used the old field probably needs attention; I can see some scenarios where the existing code might still compile after the change, but mean something different, and therefore introduce a bug that would have been avoided if you'd changed the name and forced every usage to be visited)
I have declared an enum like so in GDScript:
enum State = { STANDING, WALKING, RUNNING }
I want to get a random variant of this enum without mentioning all variants of it so that I can add more variants to the enum later without changing the code responsible for getting a random variant.
So far, I've tried this:
State.get(randi() % State.size())
And this:
State[randi() % State.size()]
Neither work. The former gives me Null, and the latter gives me the error "Invalid get index '2' (on base: 'Dictionary')."
How might I go about doing this in a way that actually works?
This can be achieved the following way:
State.keys()[randi() % State.size()]
This works because keys() converts the State dictionary to an array, which can be indexed using [].
As you can see in the title , I'd appreciate it if somebody can tell the usage of the Class .
There's a inside enum Type ,how to use it?
public static enum Type {
BETWEEN(2, "IsBetween", "Between"), IS_NOT_NULL(0, "IsNotNull", "NotNull"), IS_NULL(0, "IsNull", "Null"), LESS_THAN(
"IsLessThan", "LessThan"), LESS_THAN_EQUAL("IsLessThanEqual", "LessThanEqual"), GREATER_THAN("IsGreaterThan",
"GreaterThan"), GREATER_THAN_EQUAL("IsGreaterThanEqual", "GreaterThanEqual"), BEFORE("IsBefore", "Before"), AFTER(
"IsAfter", "After"), NOT_LIKE("IsNotLike", "NotLike"), LIKE("IsLike", "Like"), STARTING_WITH("IsStartingWith",
"StartingWith", "StartsWith"), ENDING_WITH("IsEndingWith", "EndingWith", "EndsWith"), NOT_CONTAINING(
"IsNotContaining", "NotContaining", "NotContains"), CONTAINING("IsContaining", "Containing", "Contains"), NOT_IN(
"IsNotIn", "NotIn"), IN("IsIn", "In"), NEAR("IsNear", "Near"), WITHIN("IsWithin", "Within"), REGEX(
"MatchesRegex", "Matches", "Regex"), EXISTS(0, "Exists"), TRUE(0, "IsTrue", "True"), FALSE(0, "IsFalse",
"False"), NEGATING_SIMPLE_PROPERTY("IsNot", "Not"), SIMPLE_PROPERTY("Is", "Equals");
// Need to list them again explicitly as the order is important
// (esp. for IS_NULL, IS_NOT_NULL)
private static final List<Part.Type> ALL = Arrays.asList(IS_NOT_NULL, IS_NULL, BETWEEN, LESS_THAN, LESS_THAN_EQUAL,
GREATER_THAN, GREATER_THAN_EQUAL, BEFORE, AFTER, NOT_LIKE, LIKE, STARTING_WITH, ENDING_WITH, NOT_CONTAINING,
CONTAINING, NOT_IN, IN, NEAR, WITHIN, REGEX, EXISTS, TRUE, FALSE, NEGATING_SIMPLE_PROPERTY, SIMPLE_PROPERTY);
...}
Part is internal to Spring Data. It is not intended to be used by client code. So if you don't implement your own Spring Data Modul you shouldn't use it at all nor anything inside it.
A Part is basically an element of an AST that will probably result in an element of a where clause or equivalent depending on the store in use.
E.g. if you have a method findByNameAndDobBetween(String, Date, Date) parsing the method name will result in two parts. One for the name condition and one for the DOB between condition.
The type enum lists all the different types of conditions that are possible.
The parameters of the elements are the number of method arguments required and (possibly multiple) Strings that identify this type inside a method name.
I have a formflow dialog that has the following question defined as an enum
public enum PreviousOwnerOptions
{
[Describe("Owned from new")]
[Terms("0", "new", ".*[O|o]wned from new")]
OwnedFromNew = 0,
[Terms("1", "One")]
One,
[Terms("2", "Two")]
Two,
[Terms("3", "Three")]
Three,
[Terms("4", "Four")]
Four,
[Terms("5", "Five")]
Five,
[Terms("6", "Six")]
Six,
[Describe("More than six")]
[Terms(".*More than six", "more")]
MoreThanSix
}
Here is how the question appears to the user...
The problem I have is that if you enter, say, the number "3" as an answer, then the response is this...
It looks like the bot is not sure whether I meant the answer in position 3, or the answer "Three". I thought the Terms attribute would take care of that clarification?
How can I fix this please?
This is happening because of a combination of 2 things.
First, you are trying to use the 0 Enum values on what appears to be a non-nullable field. The 0 value is reserved for null in this case. From the formflow docs page:
Any of the data types may be nullable, which you can use to model that the field does not have a value. If a form field is based on an enumeration property that is not nullable, the value 0 in the enumeration represents null (i.e., indicates that the field does not have a value), and you should start your enumeration values at 1. FormFlow ignores all other property types and methods.
The second piece of this is that since you are using the numerical values of 1,2,3 etc in your Terms attribute like [Terms("1", "One")] by default formflow will try to align these values with the correct enumeration. So what I think is happening is that it is letting you select "3" as you used in your example and since 3 is one of your terms [Terms("3", "Three")] it offeres you that options. but in the zero index enumeration values since 0 is reserved, the actual enumeration value of [Terms("2", "Two")] Two, is 3. So it doesn't know which you mean.
So in order to get this to work using these terms would be like this:
public enum PreviousOwnerOptions
{
[Terms("1", "One")]
One=1,
[Terms("2", "Two")]
Two,
[Terms("3", "Three")]
Three,
[Terms("4", "Four")]
Four,
[Terms("5", "Five")]
Five,
[Terms("6", "Six")]
Six,
[Describe("More than six")]
[Terms(".*More than six", "more")]
MoreThanSix,
[Describe("Owned from new")]
[Terms("new", ".*[O|o]wned from new")]
OwnedFromNew
}
Warning/Disclaimer: This is a basic JavaScript question, but I've gone through a bunch of iterations in my code, much Googling, and I'm having trouble wrapping my head around how to proceed.
I have data in three columns in a CSV file: a political candidate's name, their party, and their approval rating.
I've created a bubble chart/force layout, similar to this. Candidates are represented as bubbles. Users can select to see candidates organized in one big blob, or they can click to see the bubbles organize themselves by party. I have some <div> elements that pop up under each grouping of same-party candidates. What I'd like to do now is to have each party-specific <div> element display the total approval rating all its same-party candidates get, combined. (So, in Excel, a =SUMIF().)
To do this: I'm creating a function(party) which should, in principle, return that conditional sum. Here's what it looks like when I'm calling it for candidates with no party affiliation:
d3.select("#text-NONE")
.text(label_party("N/A"));
"N/A" is the string found in the CSV file.
And the function itself:
function label_party(party) {
var party_total = 0;
function party(d) {
if (d.party[i] == "party") {
party_total+= d.y2012[i];
};
};
return party_total;
};
Both of the above are happening outside of the d3.csv() call. My main Q: how can I set up a conditional sum over two columns in a CSV? At the moment, it's simply returning 0 - so it's skipping my loop, though I don't know why.