Skip to content

Commit

Permalink
Moved some stuff to new Interpolation class for now, will decided if …
Browse files Browse the repository at this point in the history
…to actually do this.
  • Loading branch information
brett-smith committed Apr 10, 2024
1 parent a03a072 commit cd26a44
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 49 deletions.
46 changes: 0 additions & 46 deletions src/main/java/com/sshtools/jini/INI.java
Original file line number Diff line number Diff line change
Expand Up @@ -1241,50 +1241,4 @@ public Entry<String, V> next() {
}

}
@SuppressWarnings("unchecked")
public static Function<String, String> compound(Function<String, String>... sources) {
return (k) -> {
for(var src : sources) {
var v = src.apply(k);
if(v != null)
return v;
}
return null;
};
}

public static Function<String, String> systemProperties() {
return (k) -> {
if(k.startsWith("sys.")) {
return System.getProperty(k.substring(4));
}
else if(k.startsWith("env.")) {
return System.getenv(k.substring(4));
}
else
return null;
};
}

public static String str(String text, Function<String, String> source) {
var pattern = Pattern.compile("\\$\\{(.*?)\\}");
var matcher = pattern.matcher(text);
var builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
var variable = matcher.group(1);
var replacement = source.apply(variable);
builder.append(text.substring(i, matcher.start()));
if (replacement == null) {
throw new IllegalArgumentException(MessageFormat.format("Unknown string variable $\\{{0}\\}", variable));
} else {
builder.append(replacement);
}
i = matcher.end();

}
builder.append(text.substring(i, text.length()));
text = builder.toString();
return text;
}
}
3 changes: 0 additions & 3 deletions src/main/java/com/sshtools/jini/INIPreferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ public INIStoreBuilder withFailOnParsingError(boolean failOnParsingError) {
}

public INIStoreBuilder withMode(Mode mode) {
if (customRoot.isPresent() && scope != Scope.CUSTOM) {
throw new IllegalArgumentException("A custom root has been set. Scope may not be used.");
}
this.mode = mode;
return this;
}
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/com/sshtools/jini/Interpolation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.sshtools.jini;

import java.text.MessageFormat;
import java.util.function.Function;
import java.util.regex.Pattern;

public class Interpolation {

@SuppressWarnings("unchecked")
public static Function<String, String> compound(Function<String, String>... sources) {
return (k) -> {
for(var src : sources) {
var v = src.apply(k);
if(v != null)
return v;
}
return null;
};
}

public static Function<String, String> systemProperties() {
return (k) -> {
if(k.startsWith("sys.")) {
return System.getProperty(k.substring(4));
}
else if(k.startsWith("env.")) {
return System.getenv(k.substring(4));
}
else
return null;
};
}

public static String str(String text, Function<String, String> source) {
var pattern = Pattern.compile("\\$\\{(.*?)\\}");
var matcher = pattern.matcher(text);
var builder = new StringBuilder();
int i = 0;
while (matcher.find()) {
var variable = matcher.group(1);
var replacement = source.apply(variable);
builder.append(text.substring(i, matcher.start()));
if (replacement == null) {
throw new IllegalArgumentException(MessageFormat.format("Unknown string variable $\\{{0}\\}", variable));
} else {
builder.append(replacement);
}
i = matcher.end();

}
builder.append(text.substring(i, text.length()));
text = builder.toString();
return text;
}
}
27 changes: 27 additions & 0 deletions src/test/java/com/sshtools/jini/INITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.Reader;
import java.io.UncheckedIOException;
Expand Down Expand Up @@ -113,6 +114,32 @@ public void testReadFromFile() throws Exception {
assertBasicInsensitive(ini);
}

@Test
public void testReadFromStream() throws Exception {
var ini = INI.fromInput(new ByteArrayInputStream(getBasicIni().getBytes()));
assertBasic(ini);
assertBasicOrder(ini);
assertBasicInsensitive(ini);
}

@Test
public void testReadOnly() throws Exception {
assertThrows(UnsupportedOperationException.class, () -> {
var ini = INI.fromString(getBasicIni()).readOnly();
ini.put("abc", true);
});
}

@Test
public void testEmpty() throws Exception {
assertThrows(UnsupportedOperationException.class, () -> {
var ini = INI.empty();
assertEquals(0, ini.keys().size());
assertEquals(0, ini.sections().size());
ini.put("abc", true);
});
}

@Test
public void testFailReadFromFile() throws Exception {
assertThrows(UncheckedIOException.class, () -> INI.fromFile(Paths.get(UUID.randomUUID().toString())));
Expand Down

0 comments on commit cd26a44

Please sign in to comment.