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

Add typed getter for session #320

Open
gdaniel opened this issue Jan 25, 2021 · 1 comment
Open

Add typed getter for session #320

gdaniel opened this issue Jan 25, 2021 · 1 comment
Assignees
Labels
Enhancement New feature or request Low Priority

Comments

@gdaniel
Copy link
Member

gdaniel commented Jan 25, 2021

Session is implemented as a Map<Object, Object>, so we have a single way to access session values: session.get(key), which returns an Object.

Most of the time we know what we are storing in the session though, and we have to cast the result of get:

String myString = (String) context.getSession().get("myKey");

While this is fine for simple types it is annoying for complex ones, especially when accessed in transitions where we typically want a compact syntax:

myState.body(...)
    .next()
    .when(((MyComplexType)context.getSession().get("myKey")).getX() == 2).moveTo(...)

This could be simplified by implementing typed getters for the session:

myState.body(...)
    .next()
    .when(context.getSession().get("myKey", MyComplexType.class).getX() == 2).moveTo(...)

Additional getters could be provided for basic types: getAsString, getAsInt, etc

@gdaniel gdaniel added Enhancement New feature or request Low Priority labels Jan 25, 2021
@gdaniel gdaniel self-assigned this Jan 25, 2021
@gdaniel
Copy link
Member Author

gdaniel commented Jan 25, 2021

See this piece of code:

private static class MyMap<K, V> extends HashMap<K, V> {

        public String getAsString(K key) {
            return get(key, String.class);
        }

        public Integer getAsInt(K key) {
            return get(key, Integer.class);
        }

        public <T> T get(K key, Class<T> type) {
            Object o = this.get(key);
            return (T) o;
        }

    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement New feature or request Low Priority
Projects
None yet
Development

No branches or pull requests

1 participant