Map To Struct

Want more objective way to play with INI? Cool.

  1. Name = Unknwon
  2. age = 21
  3. Male = true
  4. Born = 1993-01-01T20:17:05Z
  5. [Note]
  6. Content = Hi is a good man!
  7. Cities = HangZhou, Boston
  1. type Note struct {
  2. Content string
  3. Cities []string
  4. }
  5. type Person struct {
  6. Name string
  7. Age int `ini:"age"`
  8. Male bool
  9. Born time.Time
  10. Note
  11. Created time.Time `ini:"-"`
  12. }
  13. func main() {
  14. cfg, err := ini.Load("path/to/ini")
  15. // ...
  16. p := new(Person)
  17. err = cfg.MapTo(p)
  18. // ...
  19. // Things can be simpler.
  20. err = ini.MapTo(p, "path/to/ini")
  21. // ...
  22. // Just map a section? Fine.
  23. n := new(Note)
  24. err = cfg.Section("Note").MapTo(n)
  25. // ...
  26. }

Can I have default value for field? Absolutely.

Assign it before you map to struct. It will keep the value as it is if the key is not presented or got wrong type.

  1. // ...
  2. p := &Person{
  3. Name: "Joe",
  4. }
  5. // ...

It’s really cool, but what’s the point if you can’t give me my file back from struct?

Reflect From Struct

Why not?

  1. type Embeded struct {
  2. Dates []time.Time `delim:"|" comment:"Time data"`
  3. Places []string `ini:"places,omitempty"`
  4. None []int `ini:",omitempty"`
  5. }
  6. type Author struct {
  7. Name string `ini:"NAME"`
  8. Male bool
  9. Age int `comment:"Author's age"`
  10. GPA float64
  11. NeverMind string `ini:"-"`
  12. *Embeded `comment:"Embeded section"`
  13. }
  14. func main() {
  15. a := &Author{"Unknwon", true, 21, 2.8, "",
  16. &Embeded{
  17. []time.Time{time.Now(), time.Now()},
  18. []string{"HangZhou", "Boston"},
  19. []int{},
  20. }}
  21. cfg := ini.Empty()
  22. err = ini.ReflectFrom(cfg, a)
  23. // ...
  24. }

So, what do I get?

  1. NAME = Unknwon
  2. Male = true
  3. ; Author's age
  4. Age = 21
  5. GPA = 2.8
  6. ; Embeded section
  7. [Embeded]
  8. ; Time data
  9. Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
  10. places = HangZhou,Boston

Map with ShadowLoad

If you want to map a section to a struct along with ShadowLoad, then you need to indicate allowshadow in the struct tag.

For example, suppose you have the following configuration:

  1. [IP]
  2. value = 192.168.31.201
  3. value = 192.168.31.211
  4. value = 192.168.31.221

You should define your struct as follows:

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

In case you don’t need the first two tag rules, then you can just have ini:",,allowshadow".

Other Notes On Map/Reflect

Any embedded struct is treated as a section by default, and there is no automatic parent-child relations in map/reflect feature:

  1. type Child struct {
  2. Age string
  3. }
  4. type Parent struct {
  5. Name string
  6. Child
  7. }
  8. type Config struct {
  9. City string
  10. Parent
  11. }

Example configuration:

  1. City = Boston
  2. [Parent]
  3. Name = Unknwon
  4. [Child]
  5. Age = 21

What if, yes, I’m paranoid, I want embedded struct to be in the same section. Well, all roads lead to Rome.

  1. type Child struct {
  2. Age string
  3. }
  4. type Parent struct {
  5. Name string
  6. Child `ini:"Parent"`
  7. }
  8. type Config struct {
  9. City string
  10. Parent
  11. }

Example configuration:

  1. City = Boston
  2. [Parent]
  3. Name = Unknwon
  4. Age = 21

See also Customize name and value mappers.

原文: https://ini.unknwon.io/docs/advanced/map_and_reflect