Skip to content

Feat: Adds/Nilejack/home-work09 #313

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 8 commits into from
Closed
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
102 changes: 0 additions & 102 deletions lesson_07/conditionals/src/lesson7.ts

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,51 +1,5 @@
package com.codedifferently.lesson9.dataprovider;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class DataProvider {

/**
* This class is a base class for data providers. It provides a method for parsing data from a list
* of maps.
*/
public abstract class DataProvider {
/** Returns the name of the provider. */
public abstract String getProviderName();

/** Gets a map of column types keyed by column name. */
public abstract Map<String, Class> getColumnTypeByName();

/**
* Given a list of data objects, returns the list with values converted to the appropriate type.
*
* @param data A list of data objects containing values keyed by column name.
* @return A new list with object values converted to the appropriate type.
*/
public List<Map<String, Object>> parseData(List<Map<String, String>> data) throws Exception {
Map<String, Class> columnTypeByName = getColumnTypeByName();
return data.stream().map(row -> parseRow(columnTypeByName, row)).collect(Collectors.toList());
}

/**
* Parses a single row of data using the provided column types mapping.
*
* @param columnTypeByName A map containing the type of a column keyed by its name.
* @param row A bag of values keyed by column name.
* @return A new list of data values converted to the appropriate type.
*/
private Map<String, Object> parseRow(
Map<String, Class> columnTypeByName, Map<String, String> row) {
var parsedRow = new java.util.HashMap<String, Object>();
row.forEach(
(key, value) -> {
Class type = columnTypeByName.get(key);
try {
parsedRow.put(key, type.getConstructor(String.class).newInstance(value));
} catch (Exception e) {
throw new RuntimeException(
"Error parsing data for " + key + " as type " + type.getName());
}
});
return parsedRow;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.codedifferently.lesson9.dataprovider;

import java.util.Map;

import org.springframework.stereotype.Service;

@Service
public class NileJackProvider extends DataProvider {
public String getProviderName() {
return "nilejack";
}

@SuppressWarnings("rawtypes")
public Map<String, Class> getColumnTypeByName() {
return Map.of(
"column1", Float.class,
"column2", Integer.class,
"column3", Short.class,
"column4", String.class,
"column5", Boolean.class,
"column6", Double.class,
"column7", Long.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.codedifferently.lesson9.dataprovider;

import java.util.Map;

import org.springframework.stereotype.Service;

@Service
public class NileJackProvider extends DataProvider {
public String getProviderName() {
return "nilejack";
}

public Map<String, Class> getColumnTypeByName() {
return Map.of(
"column1", Float.class,
"column2", Integer.class,
"column3", Short.class,
"column4", String.class,
"column5", Boolean.class,
"column6", Double.class,
"column7", Long.class);
}
}
92 changes: 92 additions & 0 deletions lesson_09/types/types_app/src/main/resources/data/nilejack.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
[
{
"column1": "2.788476E38",
"column2": "23906",
"column3": "1.951334947469109E306",
"column4": "4vynlcbme89",
"column5": "true",
"column6": "1946511983",
"column7": "871716420668780160"
},
{
"column1": "2.9218724E38",
"column2": "5982",
"column3": "1.6613559878792952E308",
"column4": "kcy91i4",
"column5": "true",
"column6": "467285300",
"column7": "717033365520104960"
},
{
"column1": "2.6791138E38",
"column2": "3087",
"column3": "1.4531283019824207E307",
"column4": "5gdku",
"column5": "false",
"column6": "1339783318",
"column7": "8609205761188083712"
},
{
"column1": "2.7654423E38",
"column2": "22857",
"column3": "1.3125501238039668E308",
"column4": "2ab3nxz",
"column5": "false",
"column6": "1166125928",
"column7": "3850529467803900928"
},
{
"column1": "7.978512E37",
"column2": "30614",
"column3": "1.0016443609479493E308",
"column4": "jh490kp25z8",
"column5": "true",
"column6": "684136174",
"column7": "2530680619863128064"
},
{
"column1": "5.8039073E37",
"column2": "4886",
"column3": "1.3705152061964327E308",
"column4": "ak5we9tfr",
"column5": "false",
"column6": "728654337",
"column7": "1671706500207470592"
},
{
"column1": "5.139036E37",
"column2": "9019",
"column3": "1.2523590249164425E308",
"column4": "5tefqwblim3",
"column5": "true",
"column6": "14150416",
"column7": "4871306662885839872"
},
{
"column1": "2.6188858E37",
"column2": "2398",
"column3": "1.444754024326182E308",
"column4": "w8hfk7nl1s",
"column5": "false",
"column6": "2000776131",
"column7": "8280646876683382784"
},
{
"column1": "3.1079513E38",
"column2": "32074",
"column3": "9.799167831142151E307",
"column4": "glx5ze96mk",
"column5": "false",
"column6": "2134631706",
"column7": "3840235999993978368"
},
{
"column1": "1.1965353E38",
"column2": "30848",
"column3": "1.0643709914035035E308",
"column4": "s51gxi7hm3",
"column5": "false",
"column6": "1598587760",
"column7": "8883551926143157248"
}
]
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.codedifferently.lesson9;

import static org.assertj.core.api.Assertions.assertThat;

import com.codedifferently.lesson9.dataprovider.DataProvider;
import com.codedifferently.lesson9.loader.DataFileLoader;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;

import com.codedifferently.lesson9.dataprovider.DataProvider;
import com.codedifferently.lesson9.loader.DataFileLoader;

@SpringBootTest
@ContextConfiguration(classes = Lesson9.class)
@ExtendWith(SoftAssertionsExtension.class)
Expand Down
Loading