Skip to content

Commit

Permalink
build: moved connectors to core
Browse files Browse the repository at this point in the history
  • Loading branch information
jruaux committed Nov 7, 2024
1 parent 9f40124 commit 5d7b594
Show file tree
Hide file tree
Showing 39 changed files with 2,329 additions and 0 deletions.
19 changes: 19 additions & 0 deletions core/riot-faker/gradle.properties
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
33 changes: 33 additions & 0 deletions core/riot-faker/riot-faker.gradle
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'))
}
}
}
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;
}

}
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"));
}

}
19 changes: 19 additions & 0 deletions core/riot-file/gradle.properties
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
36 changes: 36 additions & 0 deletions core/riot-file/riot-file.gradle
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'))
}
}
}
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();
}

}
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 core/riot-file/src/main/java/com/redis/riot/file/AwsOptions.java
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);
}

}
Loading

0 comments on commit 5d7b594

Please sign in to comment.