0

I am still quite new to programming and I really hope anyone can help me. I would like to create a Map from a Yaml file. The problem is, I would like to make it optional which parameters are configured, so I do not want to create a class, in which all possible parameters are created as variables. I have used snakeyaml before, however as far as I know this is not an option in snakeyaml.

An example for my yaml file would look like this:

description: linter for microservices
meta:
  pciScope: false
image:
  name: helmcube
  tag: 2.5.4 
service:
  type: 45
deployment:
  replicaCount: 2

Do any of you have an idea how this could be realised? I have already searched for hours and could not find anything concerning that issue. I hope anyone can help

Regards

Marie

m-schie
  • 1
  • 1
  • Wouldn't it suffice if parameters that are not given are simply `null` in the resulting object? – flyx Dec 10 '20 at 20:08

1 Answers1

0

Would getting a Properties instance be sufficient for you? Instead of creating your own Class with defined variables.

If you use Spring,

public class YamlPropertySourceFactory implements PropertySourceFactory {

    @Override
    public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) 
      throws IOException {
        YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
        factory.setResources(encodedResource.getResource());

        Properties properties = factory.getObject();

        return new PropertiesPropertySource(encodedResource.getResource().getFilename(), properties);
    }
}

https://www.baeldung.com/spring-yaml-propertysource

What's the best way to load a yaml file to Map(not an environment configuration file) in spring boot web project?

--- Edited ---

You now have a Properties, which you can in turn get a Hashmap out of:

Using external Lib Guava,

com.google.common.collect.Maps.fromProperties(Properties)

Or the Java Way:

    Map properties = new Properties();
    HashMap<String, String> map = new HashMap<String, String>(properties);

This stackoverflow shows you how to convert Properties into HashMap:

Converting java.util.Properties to HashMap<String,String>

Once you have a hasmap of configurations(Strings), you could compare against another hashamap of configurations (Strings)

If this does not help you, kindly update your question with clear incentives/example of what you are trying to achieve.

JCompetence
  • 6,997
  • 3
  • 19
  • 26
  • Thank you for your suggestion @Susan Mustafa. Yes I do use Spring, but as I understand it, I would still need to create variables, if I would use a properties instance. The problem is I would like to compare what has been configured in a config file to what can be configured according to a list of possible parameters and I would rather not create an individual variable for each of the over 100 possible parameters. – m-schie Dec 10 '20 at 14:28
  • @m-schie I really would like to understand your requirement. Could you clarify in the question, what you mean by comparing, and how you would go about doing this comparison in your mind? So I can at least know how to help you. Idealy, you generate a Properties x from your yml file. Properies x can give you a map. If you have another config file? you can get another map out of it. Then compare both hashmaps? I updated my answer, to show you how to get a hashmap out of properties... – JCompetence Dec 11 '20 at 15:54