Skip to content

Commit

Permalink
Rename self since it is not used widely in Go
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas mg committed Feb 1, 2013
1 parent beda974 commit 8434ff0
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 76 deletions.
8 changes: 4 additions & 4 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ package config

type SectionError string

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

type OptionError string

func (self OptionError) Error() string {
return "option not found: " + string(self)
func (e OptionError) Error() string {
return "option not found: " + string(e)
}
44 changes: 22 additions & 22 deletions option.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,63 +23,63 @@ import "errors"
//
// It returns true if the option and value were inserted, and false if the value
// was overwritten.
func (self *Config) AddOption(section string, option string, value string) bool {
self.AddSection(section) // Make sure section exists
func (c *Config) AddOption(section string, option string, value string) bool {
c.AddSection(section) // Make sure section exists

if section == "" {
section = DEFAULT_SECTION
}

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

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

return !ok
}

// RemoveOption removes a option and value from the configuration.
// It returns true if the option and value were removed, and false otherwise,
// including if the section did not exist.
func (self *Config) RemoveOption(section string, option string) bool {
if _, ok := self.data[section]; !ok {
func (c *Config) RemoveOption(section string, option string) bool {
if _, ok := c.data[section]; !ok {
return false
}

_, ok := self.data[section][option]
delete(self.data[section], option)
_, ok := c.data[section][option]
delete(c.data[section], option)

return ok
}

// HasOption checks if the configuration has the given option in the section.
// It returns false if either the option or section do not exist.
func (self *Config) HasOption(section string, option string) bool {
if _, ok := self.data[section]; !ok {
func (c *Config) HasOption(section string, option string) bool {
if _, ok := c.data[section]; !ok {
return false
}

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

return okd || oknd
}

// Options returns the list of options available in the given section.
// It returns an error if the section does not exist and an empty list if the
// section is empty. Options within the default section are also included.
func (self *Config) Options(section string) (options []string, err error) {
if _, ok := self.data[section]; !ok {
func (c *Config) Options(section string) (options []string, err error) {
if _, ok := c.data[section]; !ok {
return nil, errors.New(SectionError(section).Error())
}

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

Expand All @@ -97,14 +97,14 @@ func (self *Config) Options(section string) (options []string, err error) {
// SectionOptions returns only the list of options available in the given section.
// Unlike Options, SectionOptions doesn't return options in default section.
// It returns an error if the section doesn't exist.
func (self *Config) SectionOptions(section string) (options []string, err error) {
if _, ok := self.data[section]; !ok {
func (c *Config) SectionOptions(section string) (options []string, err error) {
if _, ok := c.data[section]; !ok {
return nil, errors.New(SectionError(section).Error())
}

options = make([]string, len(self.data[section]))
options = make([]string, len(c.data[section]))
i := 0
for s, _ := range self.data[section] {
for s, _ := range c.data[section] {
options[i] = s
i++
}
Expand Down
10 changes: 5 additions & 5 deletions read.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func ReadDefault(fname string) (*Config, error) {

// * * *

func (self *Config) read(buf *bufio.Reader) (err error) {
func (c *Config) read(buf *bufio.Reader) (err error) {
var section, option string

for {
Expand All @@ -82,7 +82,7 @@ func (self *Config) read(buf *bufio.Reader) (err error) {
case l[0] == '[' && l[len(l)-1] == ']':
option = "" // reset multi-line value
section = strings.TrimSpace(l[1 : len(l)-1])
self.AddSection(section)
c.AddSection(section)

// No new section and no section defined so
//case section == "":
Expand All @@ -98,12 +98,12 @@ func (self *Config) read(buf *bufio.Reader) (err error) {
i := strings.IndexAny(l, "=:")
option = strings.TrimSpace(l[0:i])
value := strings.TrimSpace(stripComments(l[i+1:]))
self.AddOption(section, option, value)
c.AddOption(section, option, value)
// Continuation of multi-line value
case section != "" && option != "":
prev, _ := self.RawString(section, option)
prev, _ := c.RawString(section, option)
value := strings.TrimSpace(stripComments(l))
self.AddOption(section, option, prev+"\n"+value)
c.AddOption(section, option, prev+"\n"+value)

default:
return errors.New("could not parse line: " + l)
Expand Down
36 changes: 18 additions & 18 deletions section.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,62 +21,62 @@ package config
//
// It returns true if the new section was inserted, and false if the section
// already existed.
func (self *Config) AddSection(section string) bool {
func (c *Config) AddSection(section string) bool {
// DEFAULT_SECTION
if section == "" {
return false
}

if _, ok := self.data[section]; ok {
if _, ok := c.data[section]; ok {
return false
}

self.data[section] = make(map[string]*tValue)
c.data[section] = make(map[string]*tValue)

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

return true
}

// RemoveSection removes a section from the configuration.
// It returns true if the section was removed, and false if section did not exist.
func (self *Config) RemoveSection(section string) bool {
_, ok := self.data[section]
func (c *Config) RemoveSection(section string) bool {
_, ok := c.data[section]

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

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

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

return true
}

// HasSection checks if the configuration has the given section.
// (The default section always exists.)
func (self *Config) HasSection(section string) bool {
_, ok := self.data[section]
func (c *Config) HasSection(section string) bool {
_, ok := c.data[section]

return ok
}

// Sections returns the list of sections in the configuration.
// (The default section always exists).
func (self *Config) Sections() (sections []string) {
sections = make([]string, len(self.idSection))
func (c *Config) Sections() (sections []string) {
sections = make([]string, len(c.idSection))
pos := 0 // Position in sections

for i := 0; i < self.lastIdSection; i++ {
for section, id := range self.idSection {
for i := 0; i < c.lastIdSection; i++ {
for section, id := range c.idSection {
if id == i {
sections[pos] = section
pos++
Expand Down
34 changes: 17 additions & 17 deletions type.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (

// Bool has the same behaviour as String but converts the response to bool.
// See "boolString" for string values converted to bool.
func (self *Config) Bool(section string, option string) (value bool, err error) {
sv, err := self.String(section, option)
func (c *Config) Bool(section string, option string) (value bool, err error) {
sv, err := c.String(section, option)
if err != nil {
return false, err
}
Expand All @@ -37,8 +37,8 @@ func (self *Config) Bool(section string, option string) (value bool, err error)
}

// Float has the same behaviour as String but converts the response to float.
func (self *Config) Float(section string, option string) (value float64, err error) {
sv, err := self.String(section, option)
func (c *Config) Float(section string, option string) (value float64, err error) {
sv, err := c.String(section, option)
if err == nil {
value, err = strconv.ParseFloat(sv, 64)
}
Expand All @@ -47,8 +47,8 @@ func (self *Config) Float(section string, option string) (value float64, err err
}

// Int has the same behaviour as String but converts the response to int.
func (self *Config) Int(section string, option string) (value int, err error) {
sv, err := self.String(section, option)
func (c *Config) Int(section string, option string) (value int, err error) {
sv, err := c.String(section, option)
if err == nil {
value, err = strconv.Atoi(sv)
}
Expand All @@ -61,21 +61,21 @@ func (self *Config) Int(section string, option string) (value int, err error) {
// the beginning of this documentation.
//
// It returns an error if either the section or the option do not exist.
func (self *Config) RawString(section string, option string) (value string, err error) {
if _, ok := self.data[section]; ok {
if tValue, ok := self.data[section][option]; ok {
func (c *Config) RawString(section string, option string) (value string, err error) {
if _, ok := c.data[section]; ok {
if tValue, ok := c.data[section][option]; ok {
return tValue.v, nil
}
}
return self.RawStringDefault(option)
return c.RawStringDefault(option)
}

// RawStringDefault gets the (raw) string value for the given option from the
// DEFAULT section.
//
// It returns an error if the option does not exist in the DEFAULT section.
func (self *Config) RawStringDefault(option string) (value string, err error) {
if tValue, ok := self.data[DEFAULT_SECTION][option]; ok {
func (c *Config) RawStringDefault(option string) (value string, err error) {
if tValue, ok := c.data[DEFAULT_SECTION][option]; ok {
return tValue.v, nil
}
return "", OptionError(option)
Expand All @@ -88,8 +88,8 @@ func (self *Config) RawStringDefault(option string) (value string, err error) {
//
// It returns an error if either the section or the option do not exist, or the
// unfolding cycled.
func (self *Config) String(section string, option string) (value string, err error) {
value, err = self.RawString(section, option)
func (c *Config) String(section string, option string) (value string, err error) {
value, err = c.RawString(section, option)
if err != nil {
return "", err
}
Expand All @@ -107,9 +107,9 @@ func (self *Config) String(section string, option string) (value string, err err
noption = strings.TrimRight(noption, ")s")

// Search variable in default section
nvalue, _ := self.data[DEFAULT_SECTION][noption]
if _, ok := self.data[section][noption]; ok {
nvalue = self.data[section][noption]
nvalue, _ := c.data[DEFAULT_SECTION][noption]
if _, ok := c.data[section][noption]; ok {
nvalue = c.data[section][noption]
}
if nvalue.v == "" {
return "", OptionError(option)
Expand Down
20 changes: 10 additions & 10 deletions write.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,35 +24,35 @@ import (
// WriteFile saves the configuration representation to a file.
// The desired file permissions must be passed as in os.Open. The header is a
// string that is saved as a comment in the first line of the file.
func (self *Config) WriteFile(fname string, perm os.FileMode, header string) error {
func (c *Config) WriteFile(fname string, perm os.FileMode, header string) error {
file, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
if err != nil {
return err
}

buf := bufio.NewWriter(file)
if err = self.write(buf, header); err != nil {
if err = c.write(buf, header); err != nil {
return err
}
buf.Flush()

return file.Close()
}

func (self *Config) write(buf *bufio.Writer, header string) (err error) {
func (c *Config) write(buf *bufio.Writer, header string) (err error) {
if header != "" {
// Add comment character after of each new line.
if i := strings.Index(header, "\n"); i != -1 {
header = strings.Replace(header, "\n", "\n"+self.comment, -1)
header = strings.Replace(header, "\n", "\n"+c.comment, -1)
}

if _, err = buf.WriteString(self.comment + header + "\n"); err != nil {
if _, err = buf.WriteString(c.comment + header + "\n"); err != nil {
return err
}
}

for _, orderedSection := range self.Sections() {
for section, sectionMap := range self.data {
for _, orderedSection := range c.Sections() {
for section, sectionMap := range c.data {
if section == orderedSection {

// Skip default section if empty.
Expand All @@ -65,15 +65,15 @@ func (self *Config) write(buf *bufio.Writer, header string) (err error) {
}

// Follow the input order in options.
for i := 0; i < self.lastIdOption[section]; i++ {
for i := 0; i < c.lastIdOption[section]; i++ {
for option, tValue := range sectionMap {

if tValue.position == i {
if _, err = buf.WriteString(fmt.Sprint(
option, self.separator, tValue.v, "\n")); err != nil {
option, c.separator, tValue.v, "\n")); err != nil {
return err
}
self.RemoveOption(section, option)
c.RemoveOption(section, option)
break
}
}
Expand Down

0 comments on commit 8434ff0

Please sign in to comment.