|
| 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