Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
<aerospike-client-jdk8.version>9.0.5</aerospike-client-jdk8.version>
<aerospike-reactor.version>9.0.5</aerospike-reactor.version>
<commons-lang3.version>3.18.0</commons-lang3.version>
<jackson-dataformat-yaml.version>2.19.1</jackson-dataformat-yaml.version>
<snakeyaml.version>2.6</snakeyaml.version>
<jackson-databind.version>2.21.2</jackson-databind.version>
<lombok.version>1.18.38</lombok.version>
<reactor-test.version>3.7.7</reactor-test.version>
<junit-jupiter.version>5.11.4</junit-jupiter.version>
Expand Down Expand Up @@ -101,12 +102,18 @@
<version>${commons-lang3.version}</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-yaml -->
<!-- Needed to parse the options file -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson-dataformat-yaml.version}</version>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
<version>${snakeyaml.version}</version>
</dependency>

<!-- Jackson databind for JSON comparison in tests -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-databind.version}</version>
<scope>test</scope>
</dependency>

<dependency>
Expand Down
48 changes: 34 additions & 14 deletions src/main/java/com/aerospike/mapper/tools/AbstractBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand All @@ -23,12 +24,14 @@
import com.aerospike.client.policy.ScanPolicy;
import com.aerospike.mapper.annotations.AerospikeRecord;
import com.aerospike.mapper.tools.ClassCache.PolicyType;
import com.aerospike.mapper.tools.configuration.BinConfig;
import com.aerospike.mapper.tools.configuration.ClassConfig;
import com.aerospike.mapper.tools.configuration.Configuration;
import com.aerospike.mapper.tools.utils.TypeUtils;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.TypeDescription;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

public abstract class AbstractBuilder<T extends IBaseAeroMapper> {
private final T mapper;
Expand Down Expand Up @@ -121,34 +124,48 @@ public AbstractBuilder<T> withConfigurationFile(File file) throws IOException {
}

public AbstractBuilder<T> withConfigurationFile(File file, boolean allowsInvalid) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Configuration configuration = objectMapper.readValue(file, Configuration.class);
this.loadConfiguration(configuration, allowsInvalid);
return this;
try (InputStream fis = new FileInputStream(file)) {
return this.withConfigurationFile(fis, allowsInvalid);
}
}

public AbstractBuilder<T> withConfigurationFile(InputStream ios) throws IOException {
return this.withConfigurationFile(ios, false);
}

public AbstractBuilder<T> withConfigurationFile(InputStream ios, boolean allowsInvalid) throws IOException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Configuration configuration = objectMapper.readValue(ios, Configuration.class);
Yaml yaml = createYamlParser();
Configuration configuration = yaml.load(ios);
this.loadConfiguration(configuration, allowsInvalid);
return this;
}

public AbstractBuilder<T> withConfiguration(String configurationYaml) throws JsonProcessingException {
public AbstractBuilder<T> withConfiguration(String configurationYaml) {
return this.withConfiguration(configurationYaml, false);
}

public AbstractBuilder<T> withConfiguration(String configurationYaml, boolean allowsInvalid) throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
Configuration configuration = objectMapper.readValue(configurationYaml, Configuration.class);
public AbstractBuilder<T> withConfiguration(String configurationYaml, boolean allowsInvalid) {
Yaml yaml = createYamlParser();
Configuration configuration = yaml.load(configurationYaml);
this.loadConfiguration(configuration, allowsInvalid);
return this;
}


private static Yaml createYamlParser() {
Constructor constructor = new Constructor(Configuration.class, new LoaderOptions());

TypeDescription configDesc = new TypeDescription(Configuration.class);
configDesc.addPropertyParameters("classes", ClassConfig.class);
constructor.addTypeDescription(configDesc);

TypeDescription classConfigDesc = new TypeDescription(ClassConfig.class);
classConfigDesc.substituteProperty("class", String.class, "getClassName", "setClassName");
classConfigDesc.addPropertyParameters("bins", BinConfig.class);
constructor.addTypeDescription(classConfigDesc);

return new Yaml(constructor);
}

public AbstractBuilder<T> withClassConfigurations(ClassConfig classConfig, ClassConfig ...classConfigs) {
Configuration configuration = new Configuration();
configuration.add(classConfig);
Expand All @@ -161,6 +178,9 @@ public AbstractBuilder<T> withClassConfigurations(ClassConfig classConfig, Class
}

private void loadConfiguration(@NotNull Configuration configuration, boolean allowsInvalid) {
if (configuration == null) {
throw new AerospikeException("YAML configuration is empty or invalid");
}
for (ClassConfig config : configuration.getClasses()) {
try {
String name = config.getClassName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@
import com.aerospike.mapper.annotations.AerospikeEmbed;
import com.aerospike.mapper.annotations.AerospikeReference;
import com.aerospike.mapper.tools.ConfigurationUtils;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ClassConfig {
@JsonProperty(value = "class")
private String className;
private String namespace;
private String set;
Expand Down Expand Up @@ -124,43 +122,43 @@ public void validate() {
}
}

private void setClassName(String className) {
public void setClassName(String className) {
this.className = className;
}

private void setNamespace(String namespace) {
public void setNamespace(String namespace) {
this.namespace = namespace;
}

private void setSet(String set) {
public void setSet(String set) {
this.set = set;
}

private void setTtl(Integer ttl) {
public void setTtl(Integer ttl) {
this.ttl = ttl;
}

private void setVersion(Integer version) {
public void setVersion(Integer version) {
this.version = version;
}

private void setSendKey(Boolean sendKey) {
public void setSendKey(Boolean sendKey) {
this.sendKey = sendKey;
}

private void setMapAll(Boolean mapAll) {
public void setMapAll(Boolean mapAll) {
this.mapAll = mapAll;
}

private void setDurableDelete(Boolean durableDelete) {
public void setDurableDelete(Boolean durableDelete) {
this.durableDelete = durableDelete;
}

private void setKey(KeyConfig key) {
public void setKey(KeyConfig key) {
this.key = key;
}

private void setShortName(String shortName) {
public void setShortName(String shortName) {
this.shortName = shortName;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public Boolean getStoreAsBin() {
return storeAsBin;
}

public void setStoreAsBin(boolean value) {
public void setStoreAsBin(Boolean value) {
this.storeAsBin = value;
}

Expand Down
7 changes: 3 additions & 4 deletions src/test/java/com/aerospike/mapper/BooleanTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.aerospike.mapper.annotations.AerospikeKey;
import com.aerospike.mapper.annotations.AerospikeRecord;
import com.aerospike.mapper.tools.AeroMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Test;

Expand All @@ -28,7 +27,7 @@ public static class B {
}

@Test
public void testNumericEncoding() throws JsonProcessingException {
public void testNumericEncoding() {
UseBoolBin = false;

B b = new B();
Expand Down Expand Up @@ -57,7 +56,7 @@ public void testNumericEncoding() throws JsonProcessingException {
}

@Test
public void testObjectEncoding() throws JsonProcessingException {
public void testObjectEncoding() {
UseBoolBin = true;

B b = new B();
Expand Down Expand Up @@ -85,7 +84,7 @@ public void testObjectEncoding() throws JsonProcessingException {
}

@Test
public void testObjectByDefault() throws JsonProcessingException {
public void testObjectByDefault() {
B b = new B();
b.boolValue = true;
b.id = 1;
Expand Down
Loading