-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
2,329 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Copyright 2022-2023 The RIOT authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
project_description = RIOT Faker | ||
automatic.module.name = com.redis.riot.faker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020-2023 The RIOT authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
dependencies { | ||
implementation 'org.springframework.batch:spring-batch-infrastructure' | ||
api group: 'net.datafaker', name: 'datafaker', version: datafakerVersion | ||
} | ||
|
||
compileJava { | ||
options.compilerArgs += ["-AprojectPath=${project.group}/${project.name}"] | ||
} | ||
|
||
if (!(project.findProperty('automatic.module.name.skip') ?: false).toBoolean()) { | ||
jar { | ||
manifest { | ||
attributes('Automatic-Module-Name': project.findProperty('automatic.module.name')) | ||
} | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
core/riot-faker/src/main/java/com/redis/riot/faker/FakerItemReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.redis.riot.faker; | ||
|
||
import java.util.AbstractMap; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.Locale; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader; | ||
import org.springframework.util.Assert; | ||
import org.springframework.util.ClassUtils; | ||
|
||
import net.datafaker.Faker; | ||
|
||
/** | ||
* {@link ItemReader} that generates HashMaps using Faker. | ||
* | ||
* @author Julien Ruaux | ||
*/ | ||
public class FakerItemReader extends AbstractItemCountingItemStreamItemReader<Map<String, Object>> { | ||
|
||
public static final Locale DEFAULT_LOCALE = Locale.getDefault(); | ||
|
||
private Map<String, String> expressions = new LinkedHashMap<>(); | ||
private Locale locale = DEFAULT_LOCALE; | ||
|
||
private Faker faker; | ||
private Map<String, String> fields; | ||
|
||
public FakerItemReader() { | ||
setName(ClassUtils.getShortName(getClass())); | ||
} | ||
|
||
public void setLocale(Locale locale) { | ||
this.locale = locale; | ||
} | ||
|
||
public void setExpressions(Map<String, String> fields) { | ||
this.expressions = fields; | ||
} | ||
|
||
@Override | ||
protected synchronized void doOpen() throws Exception { | ||
Assert.notEmpty(expressions, "No field specified"); | ||
if (fields == null) { | ||
fields = expressions.entrySet().stream().map(this::normalizeField) | ||
.collect(Collectors.toMap(Entry::getKey, Entry::getValue)); | ||
} | ||
faker = new Faker(locale); | ||
} | ||
|
||
private Entry<String, String> normalizeField(Entry<String, String> field) { | ||
if (field.getValue().startsWith("#{")) { | ||
return field; | ||
} | ||
return new AbstractMap.SimpleEntry<>(field.getKey(), "#{" + field.getValue() + "}"); | ||
} | ||
|
||
@Override | ||
protected Map<String, Object> doRead() throws Exception { | ||
Map<String, Object> map = new HashMap<>(); | ||
fields.forEach((k, v) -> map.put(k, faker.expression(v))); | ||
return map; | ||
} | ||
|
||
@Override | ||
protected synchronized void doClose() { | ||
faker = null; | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
core/riot-faker/src/test/java/com/redis/riot/faker/FakerReaderTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package com.redis.riot.faker; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.ItemReader; | ||
|
||
class FakerReaderTests { | ||
|
||
public static <T> List<T> readAll(ItemReader<T> reader) throws Exception { | ||
List<T> list = new ArrayList<>(); | ||
T element; | ||
while ((element = reader.read()) != null) { | ||
list.add(element); | ||
} | ||
return list; | ||
} | ||
|
||
@Test | ||
void fakerReader() throws Exception { | ||
int count = 100; | ||
FakerItemReader reader = new FakerItemReader(); | ||
Map<String, String> fields = new LinkedHashMap<>(); | ||
fields.put("firstName", "Name.first_name"); | ||
fields.put("lastName", "Name.last_name"); | ||
reader.setExpressions(fields); | ||
reader.setMaxItemCount(count); | ||
reader.open(new ExecutionContext()); | ||
List<Map<String, Object>> items = readAll(reader); | ||
reader.close(); | ||
Assertions.assertEquals(count, items.size()); | ||
Assertions.assertTrue(items.get(0).containsKey("firstName")); | ||
Assertions.assertTrue(items.get(0).containsKey("lastName")); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
# | ||
# Copyright 2022-2023 The RIOT authors. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
project_description = RIOT File | ||
automatic.module.name = com.redis.riot.file |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* | ||
* Copyright 2020-2023 The RIOT authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
dependencies { | ||
implementation 'org.springframework.batch:spring-batch-infrastructure' | ||
api 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml' | ||
api 'org.springframework:spring-oxm' | ||
api group: 'io.awspring.cloud', name: 'spring-cloud-aws-starter-s3', version: awsVersion | ||
api group: 'com.google.cloud', name: 'spring-cloud-gcp-starter-storage', version: gcpVersion | ||
} | ||
|
||
compileJava { | ||
options.compilerArgs += ["-AprojectPath=${project.group}/${project.name}"] | ||
} | ||
|
||
if (!(project.findProperty('automatic.module.name.skip') ?: false).toBoolean()) { | ||
jar { | ||
manifest { | ||
attributes('Automatic-Module-Name': project.findProperty('automatic.module.name')) | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
core/riot-file/src/main/java/com/redis/riot/file/AbstractRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.redis.riot.file; | ||
|
||
import java.io.IOException; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.core.io.Resource; | ||
|
||
public abstract class AbstractRegistry<T> { | ||
|
||
private final Map<String, FileType> extensions = new HashMap<>(); | ||
private final Map<FileType, T> factories = new HashMap<>(); | ||
|
||
public void register(FileType fileType, T factory) { | ||
factories.put(fileType, factory); | ||
if (fileType != null) { | ||
for (String extension : fileType.getExtensions()) { | ||
extensions.put(extension, fileType); | ||
} | ||
} | ||
} | ||
|
||
protected T factory(Resource resource, FileOptions options) throws IOException { | ||
FileType fileType = fileType(resource, options); | ||
if (fileType == null) { | ||
throw new UnknownFileTypeException(resource.getFilename()); | ||
} | ||
T factory = factories.get(fileType); | ||
if (factory == null) { | ||
new UnsupportedFileTypeException(fileType); | ||
} | ||
return factory; | ||
} | ||
|
||
private FileType fileType(Resource resource, FileOptions options) { | ||
if (options.getFileType() == null) { | ||
return extensions.get(Files.extension(resource.getFilename())); | ||
} | ||
return options.getFileType(); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
core/riot-file/src/main/java/com/redis/riot/file/AwsCredentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.redis.riot.file; | ||
|
||
import lombok.ToString; | ||
|
||
@ToString | ||
public class AwsCredentials { | ||
|
||
private String accessKey; | ||
|
||
private String secretKey; | ||
|
||
public String getAccessKey() { | ||
return accessKey; | ||
} | ||
|
||
public void setAccessKey(String accessKey) { | ||
this.accessKey = accessKey; | ||
} | ||
|
||
public String getSecretKey() { | ||
return secretKey; | ||
} | ||
|
||
public void setSecretKey(String secretKey) { | ||
this.secretKey = secretKey; | ||
} | ||
|
||
} |
40 changes: 40 additions & 0 deletions
40
core/riot-file/src/main/java/com/redis/riot/file/AwsOptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package com.redis.riot.file; | ||
|
||
import java.net.URI; | ||
import java.util.Optional; | ||
|
||
import lombok.ToString; | ||
import software.amazon.awssdk.regions.Region; | ||
|
||
@ToString | ||
public class AwsOptions { | ||
|
||
private AwsCredentials credentials = new AwsCredentials(); | ||
private Optional<Region> region = Optional.empty(); | ||
private Optional<URI> endpoint = Optional.empty(); | ||
|
||
public AwsCredentials getCredentials() { | ||
return credentials; | ||
} | ||
|
||
public void setCredentials(AwsCredentials credentials) { | ||
this.credentials = credentials; | ||
} | ||
|
||
public Optional<Region> getRegion() { | ||
return region; | ||
} | ||
|
||
public void setRegion(Region region) { | ||
this.region = Optional.ofNullable(region); | ||
} | ||
|
||
public Optional<URI> getEndpoint() { | ||
return endpoint; | ||
} | ||
|
||
public void setEndpoint(URI endpoint) { | ||
this.endpoint = Optional.ofNullable(endpoint); | ||
} | ||
|
||
} |
Oops, something went wrong.