Skip to content

Commit 871ba3c

Browse files
committed
Convert int to long during value retrieval
1 parent 1c98bf1 commit 871ba3c

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/main/java/com/coditory/quark/config/ConfigValue.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ private <T> T getOrParse(ConfigValueParser parser, Class<T> type) {
7676
if (value instanceof String) {
7777
return parse(parser, type, (String) value);
7878
}
79+
if (value instanceof Number && Number.class.isAssignableFrom(type)) {
80+
return parse(parser, type, String.valueOf(value));
81+
}
7982
throw new ConfigValueConversionException(type, path.toString(), value);
8083
}
8184

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.coditory.quark.config
2+
3+
import spock.lang.Specification
4+
5+
class NumberConversionSpec extends Specification {
6+
def "should convert int to long"() {
7+
given:
8+
Config config = Config.builder()
9+
.putAll(a: value)
10+
.build()
11+
when:
12+
Long result = config.getLong("a")
13+
then:
14+
result == expected
15+
where:
16+
value || expected
17+
0 || 0L
18+
-1 || -1L
19+
12 || 12L
20+
123123 || 123123L
21+
}
22+
23+
def "should not convert float to long"() {
24+
given:
25+
Config config = Config.builder()
26+
.putAll(a: 1.2)
27+
.build()
28+
when:
29+
config.getLong("a")
30+
then:
31+
thrown(ConfigValueConversionException)
32+
}
33+
34+
def "should convert float or int to double"() {
35+
given:
36+
Config config = Config.builder()
37+
.putAll(a: value)
38+
.build()
39+
when:
40+
Double result = config.getDouble("a")
41+
then:
42+
result == expected
43+
where:
44+
value || expected
45+
0 || 0d
46+
-1 || -1d
47+
1.2 || 1.2d
48+
-123.123 || -123.123d
49+
12 || 12d
50+
123123 || 123123d
51+
}
52+
}

0 commit comments

Comments
 (0)