Skip to content

Commit cbb11a5

Browse files
committed
feat(testcontainers): add simple Tarantool 2.11.x lua config serializer
Needed for #41
1 parent a3986b9 commit cbb11a5

File tree

4 files changed

+563
-2
lines changed

4 files changed

+563
-2
lines changed

testcontainers/pom.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@
115115
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
116116
<version>${jsonschema2pojo.version}</version>
117117
<configuration>
118-
<sourceDirectory>${basedir}/src/main/resources/tarantool/Tarantool3Configuration.yaml
119-
</sourceDirectory>
118+
<sourceDirectory>${basedir}/src/main/resources/tarantool</sourceDirectory>
120119
<targetPackage>io.tarantool.autogen</targetPackage>
121120
<useInnerClassBuilders>true</useInnerClassBuilders>
122121
<generateBuilders>true</generateBuilders>
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright (c) 2025 VK DIGITAL TECHNOLOGIES LIMITED LIABILITY COMPANY
3+
* All Rights Reserved.
4+
*/
5+
6+
package org.testcontainers.containers.tarantool.config;
7+
8+
import java.io.IOException;
9+
import java.math.BigDecimal;
10+
import java.nio.charset.StandardCharsets;
11+
import java.nio.file.Files;
12+
import java.nio.file.Path;
13+
import java.util.Map;
14+
15+
import com.fasterxml.jackson.annotation.JsonInclude.Include;
16+
import com.fasterxml.jackson.core.type.TypeReference;
17+
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.fasterxml.jackson.datatype.jdk8.Jdk8Module;
19+
20+
import io.tarantool.autogen.BoxCfg;
21+
22+
public class LuaConfiguration {
23+
24+
private static final ObjectMapper OBJECT_MAPPER;
25+
26+
private static final TypeReference<Map<String, Object>> TYPE_REFERENCE;
27+
28+
private static final String CFG_CHUNK_START = "box.cfg{\n\t";
29+
30+
private static final String CFG_CHUNK_END = "}";
31+
32+
static {
33+
OBJECT_MAPPER = new ObjectMapper();
34+
OBJECT_MAPPER
35+
.setSerializationInclusion(Include.NON_NULL)
36+
.setSerializationInclusion(Include.NON_EMPTY);
37+
OBJECT_MAPPER.registerModule(new Jdk8Module());
38+
TYPE_REFERENCE = new TypeReference<>() {};
39+
}
40+
41+
public static void writeAsLuaScript(Path path, BoxCfg config) throws IOException {
42+
final byte[] configAsString = serializeConfig(config);
43+
Files.write(path, configAsString);
44+
}
45+
46+
private static byte[] serializeConfig(BoxCfg config) {
47+
final Map<String, Object> configAsMap = OBJECT_MAPPER.convertValue(config, TYPE_REFERENCE);
48+
final StringBuilder sb = new StringBuilder(CFG_CHUNK_START);
49+
for (Map.Entry<String, Object> entry : configAsMap.entrySet()) {
50+
if (entry.getValue() != null) {
51+
sb.append(entry.getKey())
52+
.append("=")
53+
.append(serializeValue(entry.getValue()))
54+
.append(',')
55+
.append("\n\t");
56+
}
57+
}
58+
sb.delete(sb.length() - 2, sb.length());
59+
if (!configAsMap.isEmpty()) {
60+
sb.append('\n');
61+
}
62+
return sb.append(CFG_CHUNK_END).toString().getBytes(StandardCharsets.UTF_8);
63+
}
64+
65+
private static String serializeValue(Object value) {
66+
if (value == null) {
67+
return "nil";
68+
}
69+
70+
if (value instanceof String | value instanceof Enum<?>) {
71+
return "'" + value + "'";
72+
}
73+
74+
if (value instanceof Boolean) {
75+
return value.toString();
76+
}
77+
78+
if (value instanceof BigDecimal) {
79+
return ((BigDecimal) value).stripTrailingZeros().toPlainString();
80+
}
81+
82+
if (value instanceof Number) {
83+
return value.toString();
84+
}
85+
86+
throw new IllegalArgumentException("Cannot serialize value of type " + value.getClass());
87+
}
88+
}

0 commit comments

Comments
 (0)