Skip to content

Commit

Permalink
add the alias for the option
Browse files Browse the repository at this point in the history
  • Loading branch information
xgfone committed Jul 9, 2019
1 parent 3e714ce commit 0578b26
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 5 deletions.
54 changes: 54 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package gconf
import (
"encoding/json"
"fmt"
"testing"
)

func ExampleConfig_Observe() {
Expand Down Expand Up @@ -317,3 +318,56 @@ func ExampleConfig() {
// [x y z]
// [7s 8s 9s]
}

func ExampleOptGroup_SetOptAlias() {
conf := New()
conf.RegisterOpt(IntOpt("newopt", "test alias").D(123))
conf.SetOptAlias("oldopt", "newopt")

fmt.Printf("newopt=%d, oldopt=%d\n", conf.GetInt("newopt"), conf.GetInt("oldopt"))
conf.Set("oldopt", 456)
fmt.Printf("newopt=%d, oldopt=%d\n", conf.GetInt("newopt"), conf.GetInt("oldopt"))

// Output:
// newopt=123, oldopt=123
// newopt=456, oldopt=456
}

func TestOptGroupAlias(t *testing.T) {
conf := New()
conf.RegisterOpt(IntOpt("int", "test alias"))
conf.SetOptAlias("opt", "int")

if opt, exist := conf.Opt("opt"); !exist || opt.Name != "int" {
t.Fail()
} else if !conf.HasOpt("opt") {
t.Fail()
} else if conf.OptIsSet("opt") {
t.Fail()
} else if !conf.HasOptAndIsNotSet("opt") {
t.Fail()
}

conf.Set("opt", 123)
if !conf.OptIsSet("opt") {
t.Fail()
} else if conf.HasOptAndIsNotSet("opt") {
t.Fail()
} else if v := conf.GetInt("opt"); v != 123 {
t.Error(v)
}

if conf.OptIsFrozen("opt") {
t.Fail()
}

conf.FreezeOpt("opt")
if !conf.OptIsFrozen("opt") {
t.Fail()
}

conf.UnfreezeOpt("opt")
if conf.OptIsFrozen("opt") {
t.Fail()
}
}
76 changes: 71 additions & 5 deletions group.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,17 @@ type OptGroup struct {

name string // short name
opts map[string]*groupOpt
alias map[string]string
frozen bool
}

func newOptGroup(conf *Config, name string) *OptGroup {
return &OptGroup{conf: conf, name: name, opts: make(map[string]*groupOpt, 16)}
return &OptGroup{
conf: conf,
name: name,
opts: make(map[string]*groupOpt, 16),
alias: make(map[string]string, 16),
}
}

// NewGroup returns a new sub-group with the name named `group`.
Expand Down Expand Up @@ -150,6 +156,9 @@ func (g *OptGroup) Opt(name string) (opt Opt, exist bool) {
name = g.fixOptName(name)
g.lock.RLock()
gopt := g.opts[name]
if gopt == nil {
gopt = g.opts[g.alias[name]] // Check the alias
}
g.lock.RUnlock()

if gopt != nil {
Expand All @@ -171,6 +180,11 @@ func (g *OptGroup) HasOpt(name string) bool {
name = g.fixOptName(name)
g.lock.RLock()
_, ok := g.opts[name]
if !ok {
if name = g.alias[name]; name != "" {
_, ok = g.opts[name]
}
}
g.lock.RUnlock()
return ok
}
Expand All @@ -183,6 +197,10 @@ func (g *OptGroup) OptIsSet(name string) bool {
g.lock.RLock()
if opt, ok := g.opts[name]; ok {
set = opt.value != nil
} else if name, ok = g.alias[name]; ok {
if opt, ok = g.opts[name]; ok {
set = opt.value != nil
}
}
g.lock.RUnlock()
return set
Expand All @@ -195,16 +213,34 @@ func (g *OptGroup) HasOptAndIsNotSet(name string) (yes bool) {
g.lock.RLock()
if opt, ok := g.opts[name]; ok {
yes = opt.value == nil
} else if name, ok = g.alias[name]; ok {
if opt, ok = g.opts[name]; ok {
yes = opt.value == nil
}
}
g.lock.RUnlock()
return
}

// SetOptAlias sets the alias of the option from old to new. So you can access
// the option named new by the old name.
func (g *OptGroup) SetOptAlias(old, new string) {
old = g.fixOptName(old)
new = g.fixOptName(new)
g.lock.Lock()
g.alias[old] = new
g.lock.Unlock()
}

func (g *OptGroup) setOptWatch(name string, watch func(interface{})) {
name = g.fixOptName(name)
g.lock.Lock()
if opt, ok := g.opts[name]; ok {
opt.watch = watch
} else if name, ok = g.alias[name]; ok {
if opt, ok = g.opts[name]; ok {
opt.watch = watch
}
}
g.lock.Unlock()
}
Expand Down Expand Up @@ -315,7 +351,13 @@ func (g *OptGroup) FreezeOpt(names ...string) {

g.lock.Lock()
for _, name := range names {
if gopt, ok := g.opts[name]; ok && !gopt.frozen {
gopt, ok := g.opts[name]
if !ok {
if alias, exist := g.alias[name]; exist {
gopt, ok = g.opts[alias]
}
}
if ok && !gopt.frozen {
gopt.frozen = true
}
}
Expand All @@ -332,7 +374,13 @@ func (g *OptGroup) UnfreezeOpt(names ...string) {

g.lock.Lock()
for _, name := range names {
if gopt, ok := g.opts[name]; ok && gopt.frozen {
gopt, ok := g.opts[name]
if !ok {
if alias, exist := g.alias[name]; exist {
gopt, ok = g.opts[alias]
}
}
if ok && gopt.frozen {
gopt.frozen = false
}
}
Expand Down Expand Up @@ -363,6 +411,10 @@ func (g *OptGroup) optIsFrozen(name string) bool {
return true
} else if gopt, ok := g.opts[name]; ok {
return gopt.frozen
} else if name, ok = g.alias[name]; ok {
if gopt, ok = g.opts[name]; ok {
return gopt.frozen
}
}
return false
}
Expand All @@ -374,7 +426,11 @@ func (g *OptGroup) parseOptValue(name string, value interface{}) (interface{}, e

opt, ok := g.opts[name]
if !ok {
return nil, ErrNoOpt
if name, ok = g.alias[name]; !ok {
return nil, ErrNoOpt
} else if opt, ok = g.opts[name]; !ok {
return nil, ErrNoOpt
}
}

// Parse the option value
Expand All @@ -393,6 +449,10 @@ func (g *OptGroup) parseOptValue(name string, value interface{}) (interface{}, e

func (g *OptGroup) setOptValue(name string, value interface{}) {
opt := g.opts[name]
if opt == nil {
name = g.alias[name]
opt = g.opts[name]
}
old := opt.value
opt.value = value
if old == nil {
Expand Down Expand Up @@ -466,7 +526,13 @@ func (g *OptGroup) Set(name string, value interface{}) {
func (g *OptGroup) Get(name string) (value interface{}) {
name = g.fixOptName(name)
g.lock.RLock()
if opt, ok := g.opts[name]; ok {
opt, ok := g.opts[name]
if !ok {
if name, ok = g.alias[name]; ok {
opt, ok = g.opts[name]
}
}
if ok {
if value = opt.value; value == nil {
value = opt.opt.Default
}
Expand Down

0 comments on commit 0578b26

Please sign in to comment.