github.com/wanlay/gorm-dm8@v1.0.5/dmr/zv.go (about)

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