magiconair / properties

Java properties scanner for Go
BSD 2-Clause "Simplified" License
323 stars 77 forks source link

Maintain order of key with FilterXXX #4

Closed dotwilbert closed 9 years ago

dotwilbert commented 9 years ago

Hi all,

Thanks for sharing your work. I find your package very useful. My properties files must also be edited by administrators. In my program I remove program generated properties and rewrite the properties file. Maintaining the order of the keys and the comments enables a friendlier file for administrators. The comments are transferred using GetComments. I could not find another way to keep the unexpanded property values, except by filtering them.

Currently the order of the keys is not maintained when any of the Filter, FilterRegexp or FilterPrefix is called. A small set of changes in properties.go will enable this.

  // FilterRegexp returns a new properties object which contains all properties
  // for which the key matches the regular expression.
  func (p *Properties) FilterRegexp(re *regexp.Regexp) *Properties {
    pp := NewProperties()
  //for k, v := range p.m {
    for _, k := range p.k {
        if re.MatchString(k) {
  //        pp.Set(k, v)
            pp.Set(k, p.m[k])
        }
    }
    return pp
  }

  // FilterPrefix returns a new properties object which contains all properties
  // for which the key starts with the prefix.
  func (p *Properties) FilterPrefix(prefix string) *Properties {
    pp := NewProperties()
  //for k, v := range p.m {
    for _, k := range p.k {
        if strings.HasPrefix(k, prefix) {
  //        pp.Set(k, v)
            pp.Set(k, p.m[k])
        }
    }
    return pp
  }

Thanks,

Wilbert

magiconair commented 9 years ago

Thanks Wilbert for finding this. I've added your change and tagged version v1.5.3 with it.