Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[smila-dev] new datamodel iterating thru Map and Seq

hi,

a little proposal regarding the new DM:

motivation/use case:
often a seq is used to store N objects of the same type, so I chose the seq as the prim. use case here but it applies to Maps as well.
with the current DM iterating thru a seq is more cumbersome than it needs to be.

when iterating thru a seq. we  need code like so:

    for (Any field : indexFields) {
      String value = ((Value) field).asString();
      ...
    }

or

    for (int i = 0; i < indexFields.size(); i++) {
      final String stringValue = indexFields.getStringValue(i);
      ...
    }

which is not so nice b/c 
- it casts the type, assuming internal knowledge not visible thru the API.
- the new for(type : iterable) is better to read/less error prone/better template-able  than the indexed version


proposal 1
I propose to either provide 
a) Any.getValue/Map/Seq() methods OR
b) push the asXXX methods from Value to Any

for invalid cases we just throw the InvalidValTypeEx.

the new code would look like so depending on the chosen solution.
    
    for (Any any : indexFields) {
      String value = any.getValue().asString();
      ...
    }

    for (Any any : indexFields) {
      String value = any.asString();
      ...
    }

moving the asXXX methods into the any interface results in less verbose code but kind of blurs the intentional design of having diff. object types.

proposal 2
in addition I suggest common converter methods in a util class or at the Seq/Map types to convert all contained any objects into one of the basic types String, Long, ...

e.g. List<String> : Map.asStrings ();


if u give me a +1 i would impl 1.a) and 2.


Thomas Menzel @ brox IT-Solutions GmbH




Back to the top