生活的道路一旦选定,就要勇敢地走到底,决不回头。

发掘积累过程的快感

首页 » BIBLE模型 » GoLang » gopkg.in/ini.v1 结构体与分区双向映射

gopkg.in/ini.v1 结构体与分区双向映射


想要使用更加面向对象的方式玩转 INI 吗?好主意。

Name = Unknwon
age = 21
Male = true
Born = 1993-01-01T20:17:05Z

[Note]
Content = Hi is a good man!
Cities = HangZhou, Boston
//=====================
type Note struct {
    Content string
    Cities  []string
}

type Person struct {
    Name string
    Age  int `ini:"age"`
    Male bool
    Born time.Time
    Note
    Created time.Time `ini:"-"`
}

func main() {
    cfg, err := ini.Load("path/to/ini")
    // ...
    p := new(Person)
    err = cfg.MapTo(p)
    // ...

    // 一切竟可以如此的简单。
    err = ini.MapTo(p, "path/to/ini")
    // ...

    // 嗯哼?只需要映射一个分区吗?
    n := new(Note)
    err = cfg.Section("Note").MapTo(n)
    // ...
}

结构的字段怎么设置默认值呢?很简单,只要在映射之前对指定字段进行赋值就可以了。如果键未找到或者类型错误,该值不会发生改变。

// ...
p := &Person{
    Name: "Joe",
}
// ...

这样玩 INI 真的好酷啊!然而,如果不能还给我原来的配置文件,有什么卵用?

从结构反射

可是,我有说不能吗?

type Embeded struct {
    Dates  []time.Time `delim:"|" comment:"Time data"`
    Places []string    `ini:"places,omitempty"`
    None   []int       `ini:",omitempty"`
}

type Author struct {
    Name      string `ini:"NAME"`
    Male      bool
    Age       int `comment:"Author's age"`
    GPA       float64
    NeverMind string `ini:"-"`
    *Embeded `comment:"Embeded section"`
}

func main() {
    a := &Author{"Unknwon", true, 21, 2.8, "",
        &Embeded{
            []time.Time{time.Now(), time.Now()},
            []string{"HangZhou", "Boston"},
            []int{},
        }}
    cfg := ini.Empty()
    err = ini.ReflectFrom(cfg, a)
    // ...
}

瞧瞧,奇迹发生了。

NAME = Unknwon
Male = true
; Author's age
Age = 21
GPA = 2.8

; Embeded section
[Embeded]
; Time data
Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
places = HangZhou,Boston

配合 ShadowLoad 进行映射

如果您希望配合 ShadowLoad 将某个分区映射到结构体,则需要指定 allowshadow 标签。

假设您有以下配置文件:

[IP]
value = 192.168.31.201
value = 192.168.31.211
value = 192.168.31.221

您应当通过如下方式定义对应的结构体:

type IP struct {
   Value    []string `ini:"value,omitempty,allowshadow"`
}

如果您不需要前两个标签规则,可以使用 ini:",,allowshadow" 进行简写。

映射/反射的其它说明

任何嵌入的结构都会被默认认作一个不同的分区,并且不会自动产生所谓的父子分区关联:

type Child struct {
    Age string
}

type Parent struct {
    Name string
    Child
}

type Config struct {
    City string
    Parent
}

示例配置文件:

City = Boston

[Parent]
Name = Unknwon

[Child]
Age = 21

很好,但是,我就是要嵌入结构也在同一个分区。好吧,你爹是李刚!

type Child struct {
    Age string
}

type Parent struct {
    Name string
    Child `ini:"Parent"`
}

type Config struct {
    City string
    Parent
}

示例配置文件:

City = Boston

[Parent]
Name = Unknwon
Age = 21
互联网信息太多太杂,各互联网公司不断推送娱乐花边新闻,SNS,微博不断转移我们的注意力。但是,我们的时间和精力却是有限的。这里是互联网浩瀚的海洋中的一座宁静与美丽的小岛,供开发者歇息与静心潜心修炼。 “Bible”是圣经,有权威的书,我们的本意就是为开发者提供真正有用的的资料。 我的电子邮件 1217179982@qq.com,您在开发过程中遇到任何问题,欢迎与我联系。
Copyright © 2024. All rights reserved. 本站由 Helay 纯手工打造. 蜀ICP备15017444号