status-im / nim-toml-serialization

Flexible TOML serialization [not] relying on run-time type information.
Apache License 2.0
37 stars 7 forks source link

parsing a table #60

Closed floppyman closed 1 year ago

floppyman commented 1 year ago

Hi I am currently using your library for a project, but i am having some trouble understanding how to create a method for parsing some values as a Table.

Is it possible you can help me understand how to do it ?

My setup is as follows:

The toml

[database]
applicationName = "name"
multiSubnetFailover = true

[database.connectionStrings.debug]
server = "localhost"
userId = "user
password = "pass"

The nim objects

type ConfigDatabaseConnectionString* = object
  server*: string
  userId*: string
  password*: string

type ConfigDatabase* = object
  applicationName*: string
  multiSubnetFailover*: bool
  connectionStrings*: Table[string, ConfigDatabaseConnectionString]

I have tried having a combination of the following proc but they all result in an error.

proc readValue*(r: var TomlReader, item: var ConfigDatabaseConnectionString) =
  parseTable(r, key):
    item.server = r.parseString(string, "server")
    item.userId = r.parseString(string, "userId")
    item.password = r.parseString(string, "password")

proc readValue*(r: var TomlReader, table: var Table[string, ConfigDatabaseConnectionString]) =
  parseTable(r, key):
    table[key].server = r.parseString(string, "server")
    table[key].userId = r.parseString(string, "userId")
    table[key].password = r.parseString(string, "password")

proc readValue*(r: var TomlReader, table: var Table[string, ConfigDatabaseConnectionString]) =
  parseTable(r, key):
    table[key].server = r.parseString(string)
    table[key].userId = r.parseString(string)
    table[key].password = r.parseString(string)

However if I copy over the example from https://nimble.directory/pkg/tomlserialization then it does not fail for that one.

So clearly I simply don't understand how to write the method.

jangko commented 1 year ago

The doc says

Dotted key. When parse into nim object, key must not a dotted key. Dotted key is supported via keyed decoding or TomlValueRef