section.go 1011 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package ini
  2. import (
  3. "fmt"
  4. "io"
  5. "git.ali33.ru/fcg-xvii/go-tools/text/config"
  6. "git.ali33.ru/fcg-xvii/go-tools/value"
  7. )
  8. func newSection() config.Section {
  9. return &Section{
  10. store: make(map[string]value.Value),
  11. }
  12. }
  13. type Section struct {
  14. store map[string]value.Value
  15. }
  16. func (s *Section) ValueSetup(name string, ptr interface{}) bool {
  17. if val, check := s.store[name]; check {
  18. return val.Setup(ptr)
  19. }
  20. return false
  21. }
  22. func (s *Section) ValueDefault(name string, defaultVal interface{}) interface{} {
  23. s.ValueSetup(name, &defaultVal)
  24. return defaultVal
  25. }
  26. func (s *Section) Value(name string) (value.Value, bool) {
  27. val, check := s.store[name]
  28. return val, check
  29. }
  30. func (s *Section) SetValue(name string, val interface{}) {
  31. s.store[name] = value.ValueOf(val)
  32. }
  33. func (s *Section) Save(w io.Writer) (err error) {
  34. for name, val := range s.store {
  35. if _, err = w.Write([]byte(fmt.Sprintf("%v = %v\n", name, val.String()))); err != nil {
  36. return err
  37. }
  38. }
  39. _, err = w.Write([]byte("\n"))
  40. return
  41. }