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

Concatenated strings as keys #25

Closed
mslinn opened this issue Jul 23, 2012 · 5 comments
Closed

Concatenated strings as keys #25

mslinn opened this issue Jul 23, 2012 · 5 comments

Comments

@mslinn
Copy link

mslinn commented Jul 23, 2012

Given the following:

repositories {
    AWS S3 : "http://jets3t.s3.amazonaws.com/maven2"
}

The key is parsed as ""AWS S3"" instead of "AWS S3", which is what I expected from reading the README.

@havocp
Copy link
Collaborator

havocp commented Jul 23, 2012

I added the following test and it passed. My guess is that you're expecting to use a string with methods that expect a path expression, see the docs for class Config in http://typesafehub.github.com/config/latest/api/

If that isn't it then it would be helpful to modify the test below to break.

    @Test
    def unquotedKeyWithWhitespace() {
        val conf = ConfigFactory.parseString("""repositories {
    AWS S3 : "http://jets3t.s3.amazonaws.com/maven2"
}""")
        assertEquals("http://jets3t.s3.amazonaws.com/maven2", conf.getObject("repositories").get("AWS S3").unwrapped.asInstanceOf[String])
        assertEquals("http://jets3t.s3.amazonaws.com/maven2", conf.getString("repositories.\"AWS S3\""))
    }

@mslinn
Copy link
Author

mslinn commented Jul 24, 2012

I've got code like this (you can see the whole file here)

val entireConfig: Config = ConfigFactory.parseURL(new URL(fetchFromUrl))

  /** Work around Config bug */
  def sanitizeKey(key: String) = key.replace("\"", " ").trim

  def makeSettings(lookup: Lookup): SortedMap[String, String] = {
    lookup.config.entrySet foreach { kv =>
      val key = kv.getKey
      val value = kv.getValue.unwrapped.toString
      keyValues.put(sanitizeKey(key), value)
    }
    SortedMap.empty[String, String] ++ (keyValues.toList.sortBy(_._1))
  }

  def apply(key: String) = {
    val sanitizedKey = sanitizeKey(key)
    val value = keyValues.get(sanitizedKey)
    if (value!=None) {
      if (!quiet && !alreadyShown.contains(sanitizedKey)) {
        alreadyShown += sanitizedKey
        println("  %s = %s".format(sanitizedKey, value.get))
      }
      value.get
    } else {
      println("Warning: %s is not defined in the config file at %s".format(sanitizedKey, fetchFromUrl))
      ""
    }
  }

@havocp
Copy link
Collaborator

havocp commented Jul 24, 2012

I think your problem is that entrySet on a Config has path expressions as keys. Try config.root.entrySet (but of course you then have a tree of maps, not a single level).

@mslinn
Copy link
Author

mslinn commented Jul 24, 2012

I'll stick with my workaround. Might be worthwhile mentioning something about this corner case in the docs.

@mslinn mslinn closed this as completed Jul 24, 2012
@havocp
Copy link
Collaborator

havocp commented Jul 24, 2012

I think it is mentioned but I'll see if
it can go in more places. To convert between paths and keys, you need to do more than your sanitizeKey does; there are utility methods joinPath and splitPath in ConfigUtil for this. or something like that, I may remember the wrong method names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants