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

Enumeration Types #23

Closed
jbarwick opened this issue Dec 4, 2013 · 1 comment
Closed

Enumeration Types #23

jbarwick opened this issue Dec 4, 2013 · 1 comment

Comments

@jbarwick
Copy link

jbarwick commented Dec 4, 2013

I'd like the ability to add "enum" classes and refer to them in a field definition. And specify in the field definition to store a string or an int.

Could create something like:

"enums": [
{
"enum_name": "ReadFlag",
"enum_values": [
{
"value": "UNREAD",
"intvalue": "0",
"string_value": "UNREAD"
},
{
"value": "READ",
"intvalue": "1",
"string_value": "READ"
},
{
"value": "UNSPECIFIED",
"intvalue": "-1",
"string_value": "UNSPECIFIED"
},
]
},

and then the resulting class would be:

public enum ReadStatus {

UNREAD, READ, UNKNOWN;

/**
 * <p>
 * Returns the enumerated value for the specified integer. If the value is
 * out of range, UNKOWN is returned.
 * <ul>
 * <li>0 - {@code UNREAD}
 * <li>1 - {@code READ}
 * </ul>
 * 
 * @param in
 * @return the enumeration
 */
public static ReadStatus forInt(int in) {
    switch (in) {
    case 0:
        return UNREAD;
    case 1:
        return READ;
    }
    return UNKNOWN;
}

/**
 * /**
 * <p>
 * Returns the enumerated value for the specified integer. If the value is
 * out of range, UNKOWN is returned.
 * <ul>
 * <li>"UNREAD" - {@code UNREAD}
 * <li>"READ" - {@code READ}
 * </ul>
 * 
 * @param in
 * @return the enumeration
 */
public static ReadStatus forString(String in) {
    if ("READ".equalsIgnoreCase(in))
        return READ;
    if ("UNREAD".equalsIgnoreCase(in))
        return UNREAD;
    return UNKNOWN;
}

/**
 * Returns an integer representation of the enumeration
 * 
 * @return an integer representation of the enumeration
 */
public int toInt() {
    switch (this) {
    case UNREAD:
        return 0;
    case READ:
        return 1;
    default:
        return -1;
    }
}

/**
 * Returns an string representation of the enumeration
 * 
 * @return an string representation of the enumeration
 */
@Override
public String toString() {
    switch (this) {
    case UNREAD:
        return "UNREAD";
    case READ:
        return "READ";
    default:
        return null;
    }
}

}

Then a reference something like:

"tables": [
{
"table_name": "TheTable",
"fields": [
{
"name": "status",
"type": "int",
"enumeration": "ReadFlag"
},

The resulting table class would be:

public class TheTable {
private ReadFlag status;
}

With the appropriate SQL inserts doing a "toInt()" or "toString()" and the appropriate cursor reads doing the "Enumeration.forInt()" or "Enumeration.forString()" calls.

What do you think? Sorry, I'm just really fond of enumerations.

Was thinking of offering some code. But in the current Code Generator's construction, do you think this would be incredibly difficult or just a bit trivial?

@jbarwick
Copy link
Author

jbarwick commented Dec 4, 2013

I just realized that this program doesn't actually create the model classes for you.

So. Never mind.

@jbarwick jbarwick closed this as completed Dec 4, 2013
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

No branches or pull requests

1 participant