Skip to content

Commit

Permalink
revel/revel#1057 code improvements except on test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
jeevatkm committed Jun 9, 2016
1 parent 6821dd3 commit 0f92ab2
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 62 deletions.
32 changes: 16 additions & 16 deletions all_test.go
Expand Up @@ -118,7 +118,7 @@ func TestInMemory(t *testing.T) {
}

// default section always exists
if c.AddSection(DEFAULT_SECTION) {
if c.AddSection(DefaultSection) {
t.Errorf("AddSection failure: true on default section insert")
}

Expand Down Expand Up @@ -187,13 +187,13 @@ func TestInMemory(t *testing.T) {

// == Test cycle

c.AddOption(DEFAULT_SECTION, "opt1", "%(opt2)s")
c.AddOption(DEFAULT_SECTION, "opt2", "%(opt1)s")
c.AddOption(DefaultSection, "opt1", "%(opt2)s")
c.AddOption(DefaultSection, "opt2", "%(opt1)s")

_, err = c.String(DEFAULT_SECTION, "opt1")
_, err = c.String(DefaultSection, "opt1")
if err == nil {
t.Errorf("String failure: no error for cycle")
} else if strings.Index(err.Error(), "cycle") < 0 {
} else if !strings.Contains(err.Error(), "cycle") {
t.Errorf("String failure: incorrect error for cycle")
}
}
Expand All @@ -218,7 +218,7 @@ func TestReadFile(t *testing.T) {
buf.WriteString(" # Let me put another comment\n")
buf.WriteString("option3= line1\n line2: \n\tline3=v # Comment multiline with := in value\n")
buf.WriteString("; Another comment\n")
buf.WriteString("[" + DEFAULT_SECTION + "]\n")
buf.WriteString("[" + DefaultSection + "]\n")
buf.WriteString("variable1=small\n")
buf.WriteString("variable2=a_part_of_a_%(variable1)s_test\n")
buf.WriteString("[secTION-2]\n")
Expand All @@ -241,7 +241,7 @@ func TestReadFile(t *testing.T) {
}

// check number of options 6 of [section-1] plus 2 of [default]
opts, err := c.Options("section-1")
opts, _ := c.Options("section-1")
if len(opts) != 8 {
t.Errorf("Options failure: wrong number of options: %d", len(opts))
}
Expand All @@ -266,8 +266,8 @@ func TestWriteReadFile(t *testing.T) {
cw.AddOption("First-Section", "option2", "2")

cw.AddOption("", "host", "www.example.com")
cw.AddOption(DEFAULT_SECTION, "protocol", "https://")
cw.AddOption(DEFAULT_SECTION, "base-url", "%(protocol)s%(host)s")
cw.AddOption(DefaultSection, "protocol", "https://")
cw.AddOption(DefaultSection, "base-url", "%(protocol)s%(host)s")

cw.AddOption("Another-Section", "useHTTPS", "y")
cw.AddOption("Another-Section", "url", "%(base-url)s/some/path")
Expand Down Expand Up @@ -298,8 +298,8 @@ func TestSectionOptions(t *testing.T) {
cw.AddOption("First-Section", "option2", "2")

cw.AddOption("", "host", "www.example.com")
cw.AddOption(DEFAULT_SECTION, "protocol", "https://")
cw.AddOption(DEFAULT_SECTION, "base-url", "%(protocol)s%(host)s")
cw.AddOption(DefaultSection, "protocol", "https://")
cw.AddOption(DefaultSection, "base-url", "%(protocol)s%(host)s")

cw.AddOption("Another-Section", "useHTTPS", "y")
cw.AddOption("Another-Section", "url", "%(base-url)s/some/path")
Expand Down Expand Up @@ -336,7 +336,7 @@ func TestSectionOptions(t *testing.T) {
t.Fatalf("SectionOptions reads wrong data: %v", options)
}

options, err = cr.SectionOptions(DEFAULT_SECTION)
options, err = cr.SectionOptions(DefaultSection)

if err != nil {
t.Fatalf("SectionOptions failure: %s", err)
Expand Down Expand Up @@ -375,18 +375,18 @@ func TestMerge(t *testing.T) {
target.Merge(source)

// Assert whether a regular option was merged from source -> target
if result, _ := target.String(DEFAULT_SECTION, "one"); result != "source1" {
if result, _ := target.String(DefaultSection, "one"); result != "source1" {
t.Errorf("Expected 'one' to be '1' but instead it was '%s'", result)
}
// Assert that a non-existent option in source was not overwritten
if result, _ := target.String(DEFAULT_SECTION, "five"); result != "5" {
if result, _ := target.String(DefaultSection, "five"); result != "5" {
t.Errorf("Expected 'five' to be '5' but instead it was '%s'", result)
}
// Assert that a folded option was correctly unfolded
if result, _ := target.String(DEFAULT_SECTION, "two_+_three"); result != "source2 + source3" {
if result, _ := target.String(DefaultSection, "two_+_three"); result != "source2 + source3" {
t.Errorf("Expected 'two_+_three' to be 'source2 + source3' but instead it was '%s'", result)
}
if result, _ := target.String(DEFAULT_SECTION, "four"); result != "4" {
if result, _ := target.String(DefaultSection, "four"); result != "4" {
t.Errorf("Expected 'four' to be '4' but instead it was '%s'", result)
}

Expand Down
31 changes: 16 additions & 15 deletions config.go
Expand Up @@ -19,16 +19,17 @@ import (
"strings"
)

// config constants
const (
// Default section name.
DEFAULT_SECTION = "DEFAULT"
DefaultSection = "DEFAULT"
// Maximum allowed depth when recursively substituing variable names.
_DEPTH_VALUES = 200
DepthValues = 200

DEFAULT_COMMENT = "# "
ALTERNATIVE_COMMENT = "; "
DEFAULT_SEPARATOR = ":"
ALTERNATIVE_SEPARATOR = "="
DefaultComment = "# "
AlternativeComment = "; "
DefaultSeparator = ":"
AlternativeSeparator = "="
)

var (
Expand Down Expand Up @@ -58,11 +59,11 @@ type Config struct {
separator string

// Sections order
lastIdSection int // Last section identifier
lastIDSection int // Last section identifier
idSection map[string]int // Section : position

// The last option identifier used for each section.
lastIdOption map[string]int // Section : last identifier
lastIDOption map[string]int // Section : last identifier

// Section -> option : value
data map[string]map[string]*tValue
Expand All @@ -80,16 +81,16 @@ type tValue struct {
//
// == Arguments
//
// comment: has to be `DEFAULT_COMMENT` or `ALTERNATIVE_COMMENT`
// separator: has to be `DEFAULT_SEPARATOR` or `ALTERNATIVE_SEPARATOR`
// comment: has to be `DefaultComment` or `AlternativeComment`
// separator: has to be `DefaultSeparator` or `AlternativeSeparator`
// preSpace: indicate if is inserted a space before of the separator
// postSpace: indicate if is added a space after of the separator
func New(comment, separator string, preSpace, postSpace bool) *Config {
if comment != DEFAULT_COMMENT && comment != ALTERNATIVE_COMMENT {
if comment != DefaultComment && comment != AlternativeComment {
panic("comment character not valid")
}

if separator != DEFAULT_SEPARATOR && separator != ALTERNATIVE_SEPARATOR {
if separator != DefaultSeparator && separator != AlternativeSeparator {
panic("separator character not valid")
}

Expand All @@ -108,17 +109,17 @@ func New(comment, separator string, preSpace, postSpace bool) *Config {
c.comment = comment
c.separator = separator
c.idSection = make(map[string]int)
c.lastIdOption = make(map[string]int)
c.lastIDOption = make(map[string]int)
c.data = make(map[string]map[string]*tValue)

c.AddSection(DEFAULT_SECTION) // Default section always exists.
c.AddSection(DefaultSection) // Default section always exists.

return c
}

// NewDefault creates a configuration representation with values by default.
func NewDefault() *Config {
return New(DEFAULT_COMMENT, DEFAULT_SEPARATOR, false, true)
return New(DefaultComment, DefaultSeparator, false, true)
}

// Merge merges the given configuration "source" with this one ("target").
Expand Down
20 changes: 20 additions & 0 deletions context.go
Expand Up @@ -30,10 +30,12 @@ type Context struct {
section string // Check this section first, then fall back to DEFAULT
}

// NewContext creates a default section and returns config context
func NewContext() *Context {
return &Context{config: NewDefault()}
}

// LoadContext loads the ini config from gives multiple conf paths
func LoadContext(confName string, confPaths []string) (*Context, error) {
ctx := NewContext()
for _, confPath := range confPaths {
Expand All @@ -51,18 +53,24 @@ func LoadContext(confName string, confPaths []string) (*Context, error) {
return ctx, nil
}

// Raw returns raw config instance
func (c *Context) Raw() *Config {
return c.config
}

// SetSection the section scope of ini config
// For e.g.: dev or prod
func (c *Context) SetSection(section string) {
c.section = section
}

// SetOption sets the value for the given key
func (c *Context) SetOption(name, value string) {
c.config.AddOption(c.section, name, value)
}

// Int returns `int` config value and if found returns true
// otherwise false
func (c *Context) Int(option string) (result int, found bool) {
result, err := c.config.Int(c.section, option)
if err == nil {
Expand All @@ -76,13 +84,17 @@ func (c *Context) Int(option string) (result int, found bool) {
return 0, false
}

// IntDefault returns `int` config value if found otherwise
// returns given default int value
func (c *Context) IntDefault(option string, dfault int) int {
if r, found := c.Int(option); found {
return r
}
return dfault
}

// Bool returns `bool` config value and if found returns true
// otherwise false
func (c *Context) Bool(option string) (result, found bool) {
result, err := c.config.Bool(c.section, option)
if err == nil {
Expand All @@ -96,27 +108,35 @@ func (c *Context) Bool(option string) (result, found bool) {
return false, false
}

// BoolDefault returns `bool` config value if found otherwise
// returns given default bool value
func (c *Context) BoolDefault(option string, dfault bool) bool {
if r, found := c.Bool(option); found {
return r
}
return dfault
}

// String returns `string` config value and if found returns true
// otherwise false
func (c *Context) String(option string) (result string, found bool) {
if r, err := c.config.String(c.section, option); err == nil {
return stripQuotes(r), true
}
return "", false
}

// StringDefault returns `string` config value if found otherwise
// returns given default string value
func (c *Context) StringDefault(option, dfault string) string {
if r, found := c.String(option); found {
return r
}
return dfault
}

// HasSection checks if the configuration has the given section.
// (The default section always exists.)
func (c *Context) HasSection(section string) bool {
return c.config.HasSection(section)
}
Expand Down
2 changes: 2 additions & 0 deletions error.go
Expand Up @@ -14,12 +14,14 @@

package config

// SectionError type string
type SectionError string

func (e SectionError) Error() string {
return "section not found: " + string(e)
}

// OptionError type string
type OptionError string

func (e OptionError) Error() string {
Expand Down
18 changes: 9 additions & 9 deletions option.go
Expand Up @@ -27,13 +27,13 @@ func (c *Config) AddOption(section string, option string, value string) bool {
c.AddSection(section) // Make sure section exists

if section == "" {
section = DEFAULT_SECTION
section = DefaultSection
}

_, ok := c.data[section][option]

c.data[section][option] = &tValue{c.lastIdOption[section], value}
c.lastIdOption[section]++
c.data[section][option] = &tValue{c.lastIDOption[section], value}
c.lastIDOption[section]++

return !ok
}
Expand All @@ -59,7 +59,7 @@ func (c *Config) HasOption(section string, option string) bool {
return false
}

_, okd := c.data[DEFAULT_SECTION][option]
_, okd := c.data[DefaultSection][option]
_, oknd := c.data[section][option]

return okd || oknd
Expand All @@ -75,18 +75,18 @@ func (c *Config) Options(section string) (options []string, err error) {

// Keep a map of option names we've seen to deduplicate.
optionMap := make(map[string]struct{},
len(c.data[DEFAULT_SECTION])+len(c.data[section]))
for s, _ := range c.data[DEFAULT_SECTION] {
len(c.data[DefaultSection])+len(c.data[section]))
for s := range c.data[DefaultSection] {
optionMap[s] = struct{}{}
}
for s, _ := range c.data[section] {
for s := range c.data[section] {
optionMap[s] = struct{}{}
}

// Get the keys.
i := 0
options = make([]string, len(optionMap))
for k, _ := range optionMap {
for k := range optionMap {
options[i] = k
i++
}
Expand All @@ -104,7 +104,7 @@ func (c *Config) SectionOptions(section string) (options []string, err error) {

options = make([]string, len(c.data[section]))
i := 0
for s, _ := range c.data[section] {
for s := range c.data[section] {
options[i] = s
i++
}
Expand Down
14 changes: 7 additions & 7 deletions section.go
Expand Up @@ -22,7 +22,7 @@ package config
// It returns true if the new section was inserted, and false if the section
// already existed.
func (c *Config) AddSection(section string) bool {
// DEFAULT_SECTION
// DefaultSection
if section == "" {
return false
}
Expand All @@ -34,8 +34,8 @@ func (c *Config) AddSection(section string) bool {
c.data[section] = make(map[string]*tValue)

// Section order
c.idSection[section] = c.lastIdSection
c.lastIdSection++
c.idSection[section] = c.lastIDSection
c.lastIDSection++

return true
}
Expand All @@ -46,16 +46,16 @@ func (c *Config) RemoveSection(section string) bool {
_, ok := c.data[section]

// Default section cannot be removed.
if !ok || section == DEFAULT_SECTION {
if !ok || section == DefaultSection {
return false
}

for o, _ := range c.data[section] {
for o := range c.data[section] {
delete(c.data[section], o) // *value
}
delete(c.data, section)

delete(c.lastIdOption, section)
delete(c.lastIDOption, section)
delete(c.idSection, section)

return true
Expand All @@ -75,7 +75,7 @@ func (c *Config) Sections() (sections []string) {
sections = make([]string, len(c.idSection))
pos := 0 // Position in sections

for i := 0; i < c.lastIdSection; i++ {
for i := 0; i < c.lastIDSection; i++ {
for section, id := range c.idSection {
if id == i {
sections[pos] = section
Expand Down

0 comments on commit 0f92ab2

Please sign in to comment.