ba0f3 / sim.nim

Parse config by defining an object

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Sim Build Status (github) made-with-nim


Sim is an utility that helps you convert a config file (only ini file supported atm) by define an object

Supported types:

  • number
  • float
  • char
  • bool (value can be string like on/off, true/false, and numbers. number less than 1 treated as false)
  • string
  • seq (list of those types above, wildcard supported, like: "student*")
  • multiple level nested objects
  • support inheritance between objects
[main]
foo = "foo"
baz = "baz"

[main/sub]
foobaz = "foobaz"

[main/sub/level2]
foobaz = "foobaz2"

NOTE: config sections and keys must use snake case

you can define a field with seq of objects, each object has itsown section in config. the key name of seq is the list of objects' name, for example:

students = "student1,student2"
; or it can writtend in wildcard
; students = "student*"
[student1]
name = John
[student2]
name = Peter

Mutil inheritanes is supported, in example below, child1 and child2 wild have same last name as dad, both are males and 11yo

children = "child*"

[dad]
fname = "William"
lname = "Smith"
age = 34
gender = M

[child1.dad]
fname = "Bob"
age = 11

[child2.child1]
fname = "John"

Why the name is Sim?

I named it as my third daughter (nothing special for you, ofcourse)

Installation

nimble  install sim

Usage

config.ini:

name = "Nim 101"
year = 2019
students = "student*"

[student1]
name = John
age = 18
[student2]
name = Peter
age = 20

foo.nim:

import sim

type
  Student = object
    name: string
    age: int

  Course = object
    name: string
    year: int
    students: seq[Student]

var cfg = loadObject[Course]("config.ini")
echo &"Welcome to `{cfg.name}`, we have cfg.students.len} students registered"

Donate

Buy me some beer https://paypal.me/ba0f3

About

Parse config by defining an object

License:MIT License


Languages

Language:Nim 100.0%