zv.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) 2000-2018, 达梦数据库有限公司.
  3. * All rights reserved.
  4. */
  5. package dm
  6. import (
  7. "strconv"
  8. "strings"
  9. )
  10. type Properties struct {
  11. innerProps map[string]string
  12. }
  13. func NewProperties() *Properties {
  14. p := Properties{
  15. innerProps: make(map[string]string, 50),
  16. }
  17. return &p
  18. }
  19. func (g *Properties) SetProperties(p *Properties) {
  20. if p == nil {
  21. return
  22. }
  23. for k, v := range p.innerProps {
  24. g.Set(strings.ToLower(k), v)
  25. }
  26. }
  27. func (g *Properties) Len() int {
  28. return len(g.innerProps)
  29. }
  30. func (g *Properties) IsNil() bool {
  31. return g == nil || g.innerProps == nil
  32. }
  33. func (g *Properties) GetString(key, def string) string {
  34. v, ok := g.innerProps[strings.ToLower(key)]
  35. if !ok || v == "" {
  36. return def
  37. }
  38. return v
  39. }
  40. func (g *Properties) GetInt(key string, def int, min int, max int) int {
  41. value, ok := g.innerProps[strings.ToLower(key)]
  42. if !ok || value == "" {
  43. return def
  44. }
  45. i, err := strconv.Atoi(value)
  46. if err != nil {
  47. return def
  48. }
  49. if i > max || i < min {
  50. return def
  51. }
  52. return i
  53. }
  54. func (g *Properties) GetBool(key string, def bool) bool {
  55. value, ok := g.innerProps[strings.ToLower(key)]
  56. if !ok || value == "" {
  57. return def
  58. }
  59. b, err := strconv.ParseBool(value)
  60. if err != nil {
  61. return def
  62. }
  63. return b
  64. }
  65. func (g *Properties) GetTrimString(key string, def string) string {
  66. value, ok := g.innerProps[strings.ToLower(key)]
  67. if !ok || value == "" {
  68. return def
  69. } else {
  70. return strings.TrimSpace(value)
  71. }
  72. }
  73. func (g *Properties) GetStringArray(key string, def []string) []string {
  74. value, ok := g.innerProps[strings.ToLower(key)]
  75. if ok || value != "" {
  76. array := strings.Split(value, ",")
  77. if len(array) > 0 {
  78. return array
  79. }
  80. }
  81. return def
  82. }
  83. //func (g *Properties) GetBool(key string) bool {
  84. // i, _ := strconv.ParseBool(g.innerProps[key])
  85. // return i
  86. //}
  87. func (g *Properties) Set(key, value string) {
  88. g.innerProps[strings.ToLower(key)] = value
  89. }
  90. func (g *Properties) SetIfNotExist(key, value string) {
  91. if _, ok := g.innerProps[strings.ToLower(key)]; !ok {
  92. g.Set(key, value)
  93. }
  94. }
  95. // 如果p有g没有的键值对,添加进g中
  96. func (g *Properties) SetDiffProperties(p *Properties) {
  97. if p == nil {
  98. return
  99. }
  100. for k, v := range p.innerProps {
  101. if _, ok := g.innerProps[strings.ToLower(k)]; !ok {
  102. g.innerProps[strings.ToLower(k)] = v
  103. }
  104. }
  105. }