Skip to content
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

Add syntax from file name parser option #573

Merged
merged 2 commits into from
Jul 25, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 14 additions & 0 deletions config/src/main/java/com/typesafe/config/ConfigParseOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package com.typesafe.config;


import com.typesafe.config.impl.ConfigImplUtil;

/**
* A set of options related to parsing.
*
Expand Down Expand Up @@ -62,6 +64,18 @@ public ConfigParseOptions setSyntax(ConfigSyntax syntax) {
this.includer, this.classLoader);
}

/**
* Set the file format. If set to null, assume {@link ConfigSyntax#CONF}.
*
* @param filename
* a configuration file name
* @return options with the syntax set
*/
public ConfigParseOptions setSyntaxFromFilename(String filename) {
ConfigSyntax syntax = ConfigImplUtil.syntaxFromExtension(filename);
return setSyntax(syntax);
}

/**
* Gets the current syntax option, which may be null for "any".
* @return the current syntax or null
Expand Down
20 changes: 20 additions & 0 deletions config/src/main/java/com/typesafe/config/impl/ConfigImplUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import com.typesafe.config.ConfigException;
import com.typesafe.config.ConfigOrigin;
import com.typesafe.config.ConfigSyntax;

/**
* Internal implementation detail, not ABI stable, do not touch.
Expand Down Expand Up @@ -233,4 +234,23 @@ static String toCamelCase(String originalName) {
}
return nameBuilder.toString();
}

/**
* Guess configuration syntax from given filename.
*
* @param filename configuration filename
* @return configuration syntax if a match is found. Otherwise, null.
*/
public static ConfigSyntax syntaxFromExtension(String filename) {
if (filename == null)
return null;
if (filename.endsWith(".json"))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this could be an "else if" I think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, I forgot to add that, will do it in a moment

return ConfigSyntax.JSON;
else if (filename.endsWith(".conf"))
return ConfigSyntax.CONF;
else if (filename.endsWith(".properties"))
return ConfigSyntax.PROPERTIES;
else
return null;
}
}
17 changes: 3 additions & 14 deletions config/src/main/java/com/typesafe/config/impl/Parseable.java
Original file line number Diff line number Diff line change
Expand Up @@ -326,17 +326,6 @@ public String toString() {
return getClass().getSimpleName();
}

private static ConfigSyntax syntaxFromExtension(String name) {
if (name.endsWith(".json"))
return ConfigSyntax.JSON;
else if (name.endsWith(".conf"))
return ConfigSyntax.CONF;
else if (name.endsWith(".properties"))
return ConfigSyntax.PROPERTIES;
else
return null;
}

private static Reader readerFromStream(InputStream input) {
return readerFromStream(input, "UTF-8");
}
Expand Down Expand Up @@ -574,7 +563,7 @@ protected Reader reader(ConfigParseOptions options) throws IOException {

@Override
ConfigSyntax guessSyntax() {
return syntaxFromExtension(input.getPath());
return ConfigImplUtil.syntaxFromExtension(input.getPath());
}

@Override
Expand Down Expand Up @@ -643,7 +632,7 @@ protected Reader reader() throws IOException {

@Override
ConfigSyntax guessSyntax() {
return syntaxFromExtension(input.getName());
return ConfigImplUtil.syntaxFromExtension(input.getName());
}

@Override
Expand Down Expand Up @@ -756,7 +745,7 @@ protected AbstractConfigObject rawParseValue(ConfigOrigin origin,

@Override
ConfigSyntax guessSyntax() {
return syntaxFromExtension(resource);
return ConfigImplUtil.syntaxFromExtension(resource);
}

static String parent(String resource) {
Expand Down
1 change: 1 addition & 0 deletions config/src/test/resources/test01.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
fromProps.abc=abc
fromProps.one=1
fromProps.bool=true
fromProps.specialChars=hello^^
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.typesafe.config.impl

import java.io.InputStreamReader

import com.typesafe.config.{ConfigException, ConfigFactory, ConfigParseOptions}
import org.hamcrest.CoreMatchers.containsString
import org.junit.Assert.{assertEquals, assertThat}
import org.junit.Test

class ParseableReaderTest extends TestUtils {

@Test
def parse(): Unit = {
val filename = "/test01.properties"
val configInput = new InputStreamReader(getClass.getResourceAsStream(filename))
val config = ConfigFactory.parseReader(configInput, ConfigParseOptions.defaults()
.setSyntaxFromFilename(filename))
assertEquals("hello^^", config.getString("fromProps.specialChars"))
}

@Test
def parseIncorrectFormat(): Unit = {
val filename = "/test01.properties"
val configInput = new InputStreamReader(getClass.getResourceAsStream(filename))
val e = intercept[ConfigException.Parse] {
ConfigFactory.parseReader(configInput)
}
assertThat(e.getMessage, containsString("Expecting end of input or a comma, got '^'"))
}
}
26 changes: 26 additions & 0 deletions config/src/test/scala/com/typesafe/config/impl/UtilTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
*/
package com.typesafe.config.impl

import com.typesafe.config.ConfigSyntax
import org.junit.Assert._
import org.junit._

Expand Down Expand Up @@ -90,4 +91,29 @@ class UtilTest extends TestUtils {
roundtripUnquoted(s)
}
}

@Test
def syntaxFromExtensionConf(): Unit = {
assertEquals(ConfigSyntax.CONF, ConfigImplUtil.syntaxFromExtension("application.conf"))
}

@Test
def syntaxFromExtensionJson(): Unit = {
assertEquals(ConfigSyntax.JSON, ConfigImplUtil.syntaxFromExtension("application.json"))
}

@Test
def syntaxFromExtensionProperties(): Unit = {
assertEquals(ConfigSyntax.PROPERTIES, ConfigImplUtil.syntaxFromExtension("application.properties"))
}

@Test
def syntaxFromExtensionUnknown(): Unit = {
assertNull(ConfigImplUtil.syntaxFromExtension("application.exe"))
}

@Test
def syntaxFromExtensionNull(): Unit = {
assertNull(ConfigImplUtil.syntaxFromExtension(null))
}
}