ba0f3 / sim.nim

Parse config by defining an object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Exception when only sections, no toplevel keys

capocasa opened this issue · comments

This is exactly 1:1 what I was looking for. Thanks!

I may have come across an issue- when there are no keys on top level, there is an exception. Reproduction:

a.nim

import sim

type Bar = object
  bar:string
  fuz:string

type Foo = object
  bar:Bar

var foo = to[Foo]("a.cfg")

for k, v in fieldPairs(foo.bar):
  echo "k: ", k, " v: ", v

a.cfg

[bar]
bar=barval
fuz=fuzval

Execute

nim c a.nim
./a

Expected behavior:

foo.bar.bar is filled with value barval, foo.bar.fuz is filled with value fuzval

Actual behavior
An exception is raised, Error: unhandled exception: Key bar not found in top level [KeyNotFoundException]

Properties of first level of object must defined in top level section, just remove [bar] section header:

bar=barval
fuz=fuzval

But what if I want multiple sections but no toplevel keys? This was meant as a simplified version of that use case.

You can use nested objects, like

type Config = object
   bar: Bar

I known it might get you confused, but Sim uses parsecfg, the config syntax is different than Windows INI