[Acceleo] Map data type? [message #1840367] |
Tue, 13 April 2021 05:50  |
Eclipse User |
|
|
|
Hi
What is a preferred way to work with maps in Acceleo?
For example, I have a Java service returning Map<String, String> or List<Map.Entry<String, String>>, etc.
Is it possible to get all keys from the map or to get a value by a key?
Actually my Java service can return any data structure. The problem is that I don't understand how to handle it in Acceleo. For now, it seems that the only way is to simulate maps by List<List<String>>. Is it right?
|
|
|
|
|
|
|
|
Re: [Acceleo] Map data type? [message #1840379 is a reply to message #1840373] |
Tue, 13 April 2021 09:49   |
Eclipse User |
|
|
|
I've found a satisfactory solution!
My Java-service returns Map<String, String>:
public Map<String, String> getProperties(EObject eObject) {
// ...
}
Which is treated as EMap:
[query public getProperties(obj : OclAny) : EMap =
invoke(
'...service.CommonService',
'getProperties(org.eclipse.emf.ecore.EObject)',
Sequence{obj})/]
And here are some helper methods:
[query public keys(arg : EMap) : Set(String) =
invoke('...service.CommonService',
'getStringMapKeys(java.util.Map)',
Sequence{arg})/]
[query public get(arg : EMap, key : String) : String =
invoke('...service.CommonService',
'getStringMapValue(java.util.Map, java.lang.String)',
Sequence{arg, key})/]
[query public merge(arg : Sequence(EMap)) : EMap =
invoke('...service.CommonService',
'mergeStringMaps(java.util.List)',
Sequence{arg})/]
And their implementation:
public Set<String> getStringMapKeys(Map<String, String> obj) {
return obj.keySet();
}
public String getStringMapValue(Map<String, String> obj, String key) {
return obj.get(key);
}
public Map<String, String> mergeStringMaps(List<Map<String, String>> objs) {
Map<String, String> result = new HashMap<>();
for (Map<String, String> obj : objs) {
obj.forEach(result::putIfAbsent);
}
return result;
}
It seems to work for me.
|
|
|
|
Powered by
FUDForum. Page generated in 0.04250 seconds