118-typesafe config

TYPESAFE CONFIG & HOCON IN EXAMPLES
HOCON: Human-Optimized Config Object Notation
Github-typesafe

1. 原始数据

nations.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
nations = {
China = {
name = "People's Republic of China"
population = 14
list = ["wuhan", "hangzhou"]
cities = [
{
population = 1
name = "wuhan in hubei"
list = [1, 2, 3]
}

{
population = 1
name = "hangzhou in zhejiang"
list = [1, 2, 3]
}
]
}


USA = {
name = "United States of America"
population = 3
list = ["LogsAngeles", "Austin"]
cities = [
{
population = 1
name = "LosAngeles"
list = [1, 2, 3]
}

{
population = 1
name = "Austin in Texas"
list = [1, 2, 3]
}
]

}

}

2. Test Config

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
public class TestConfig {

static class City {
private String name;
private int population;
private List<Integer> list;

public City(Config params) {
this.name = params.getString("name");
this.population = params.getInt("population");
this.list = params.getIntList("list");
}

public City(String name, int population, List<Integer> list) {
this.name = name;
this.population = population;
this.list = list;
}

@Override
public String toString() {
return "City{" +
"name='" + name + '\'' +
", population=" + population +
", list=" + list +
'}';
}
}


public static void main(String[] ar) {
Config originConf = ConfigFactory.parseResources("nations.conf");

//1. read value
String wuhanName = originConf.getString("nations.China.name");
int pop = originConf.getInt("nations.China.population");
List<String> list = originConf.getStringList("nations.China.list");
System.out.println("nations.China.name=" + wuhanName);
System.out.println("nations.China.population=" + pop);
System.out.println("nations.China.list=" + list);


System.out.println("-------------------------------------------\n");

//2. parse object
originConf.getConfigList("nations.China.cities").stream().map(x -> new City(x)).forEach(x -> System.out.println(x));
System.out.println("-------------------------------------------\n");


//3. read nameList
List<String> nationList = originConf.getConfig("nations")
.root()
.entrySet()
.stream()
.map(x -> x.getKey())
.collect(Collectors.toList());

System.out.println(nationList);
System.out.println("-------------------------------------------\n");


//4. update config
Config updatedConf = originConf.withValue("nations.China.list", ConfigValueFactory.fromIterable(Lists.newArrayList("Beijing", "Shanhgai")));
List<String> updatedCities = updatedConf.getStringList("nations.China.list");
List<String> originCities = originConf.getStringList("nations.China.list");
System.out.println("updatedCities="+ updatedCities);
System.out.println("originCities="+ originCities);

System.out.println("-------------------------------------------\n");



//5. fallback
City moscow = new City("Moscow", 1, Lists.newArrayList(1,2));

Config newConfig = ConfigFactory.empty()
.withValue("nations.Russia.name", ConfigValueFactory.fromAnyRef("Russia"))
.withValue("nations.Russia.population", ConfigValueFactory.fromAnyRef(1))
.withValue("nations.Russia.list", ConfigValueFactory.fromIterable(Lists.newArrayList("Moscow")));
// .withValue("nations.Russia.cities", ConfigValueFactory.fromIterable(Lists.newArrayList(moscow)));


newConfig = newConfig.withFallback(originConf);



//6. print config
ConfigRenderOptions renderOpts = ConfigRenderOptions.defaults()
.setOriginComments(false)
.setComments(false)
.setJson(false);
System.out.println("HOCON config=" + newConfig.root().render(renderOpts));

System.out.println("-------------------------------------------\n");

ConfigRenderOptions concise = ConfigRenderOptions.concise();
System.out.println("Concise config=" + newConfig.root().render(concise));
}
}

Result

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
nations.China.name=People's Republic of China
nations.China.population=14
nations.China.list=[wuhan, hangzhou]
-------------------------------------------

City{name='wuhan in hubei', population=1, list=[1, 2, 3]}
City{name='hangzhou in zhejiang', population=1, list=[1, 2, 3]}
-------------------------------------------

[USA, China]
-------------------------------------------

updatedCities=[Beijing, Shanhgai]
originCities=[wuhan, hangzhou]
-------------------------------------------

HOCON config=nations {
China {
cities=[
{
list=[
1,
2,
3
]
name="wuhan in hubei"
population=1
},
{
list=[
1,
2,
3
]
name="hangzhou in zhejiang"
population=1
}
]
list=[
wuhan,
hangzhou
]
name="People's Republic of China"
population=14
}
Russia {
list=[
Moscow
]
name=Russia
population=1
}
USA {
cities=[
{
list=[
1,
2,
3
]
name=LosAngeles
population=1
},
{
list=[
1,
2,
3
]
name="Austin in Texas"
population=1
}
]
list=[
LogsAngeles,
Austin
]
name="United States of America"
population=3
}
}

-------------------------------------------

Concise config={"nations":{"China":{"cities":[{"list":[1,2,3],"name":"wuhan in hubei","population":1},{"list":[1,2,3],"name":"hangzhou in zhejiang","population":1}],"list":["wuhan","hangzhou"],"name":"People's Republic of China","population":14},"Russia":{"list":["Moscow"],"name":"Russia","population":1},"USA":{"cities":[{"list":[1,2,3],"name":"LosAngeles","population":1},{"list":[1,2,3],"name":"Austin in Texas","population":1}],"list":["LogsAngeles","Austin"],"name":"United States of America","population":3}}}