rascal

Using Rascal IMap in vallang library


I have been trying create and insert into map in Rascal via java, but I keep getting an empty map what am I doing wrong? There is no documentation on how to use this library, this will be very useful. Here is the code:

package com.periteleios.properties;


import io.usethesource.vallang.ISourceLocation;
import io.usethesource.vallang.IValueFactory;
import io.usethesource.vallang.IMap;
import java.util.Iterator;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.fluent.Configurations;


public class Properties {
    private final IValueFactory vf;

    
    public Properties(IValueFactory vf) {
        this.vf = vf;
    }


    public IMap parsePropertiesFile(ISourceLocation location) throws Exception{
        Configurations configs = new Configurations();
        String configFile = location.getPath();
        PropertiesConfiguration config = configs.properties(configFile);

        // Access expanded properties
        Iterator<String> configKeys = config.getKeys();
        IMap properties = vf.mapWriter().done();
 
        while (configKeys.hasNext()){
            String key = configKeys.next();
            properties.put(vf.string(key), vf.string(config.getString(key)));
        }
        System.out.println("from java ----" + properties);

        return properties;
    }
}

Here is the output:

from java ----()
()

Solution

  • As IValues are immutable you have 2 options:

    1. use the map writer that accumulates values until you call done
    2. use put on an IMap, and update your map reference to the result of the put function. Since it's immutable, it returns a new map, with the new key&value added/changed.

    In general we would use the map writer, but sometimes there are cases where option 2 is desirable.

    This is option 1:

    public IMap parsePropertiesFile(ISourceLocation location) throws Exception{
        Configurations configs = new Configurations();
        String configFile = location.getPath();
        PropertiesConfiguration config = configs.properties(configFile);
    
        // Access expanded properties
        Iterator<String> configKeys = config.getKeys();
        IMapWriter properties = vf.mapWriter();
    
        while (configKeys.hasNext()){
            String key = configKeys.next();
            properties.put(vf.string(key), vf.string(config.getString(key)));
        }
        System.out.println("from java ----" + properties);
    
        return properties.done();
    }