Commits

Sebastien Binet  committed 16795e9

update for go.weekly-2011-11-09

  • Participants
  • Parent commits fa999e1
  • Tags go.weekly-2011-11-09

Comments (0)

Files changed (6)

File config/all_test.go

 
 const tmp = "/tmp/__config_test.go__garbage"
 
-
 func testGet(t *testing.T, c *Config, section string, option string,
-expected interface{}) {
+	expected interface{}) {
 	ok := false
 	switch _ := expected.(type) {
 	case string:
 
 // Creates a 'tough' configuration file and test (read) parsing.
 func TestReadFile(t *testing.T) {
-	file, err := os.Open(tmp, os.O_WRONLY|os.O_CREAT|os.O_TRUNC, 0644)
+	file, err := os.Create(tmp)
 	if err != nil {
 		t.Fatalf("Test cannot run because cannot write temporary file: " + tmp)
 	}
 
 	defer os.Remove(tmp)
 }
-

File config/option.go

 
 package config
 
-import (
-	"os"
-)
-
+import "errors"
 
 // AddOption adds a new option and value to the configuration.
 //
 	}
 
 	_, ok := self.data[section][option]
-	self.data[section][option] = nil, false
+	delete(self.data[section], option)
 
 	return ok
 }
 // 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 os.Error) {
+func (self *Config) Options(section string) (options []string, err error) {
 	if _, ok := self.data[section]; !ok {
-		return nil, os.NewError(sectionError(section).String())
+		return nil, errors.New(sectionError(section).String())
 	}
 
 	options = make([]string, len(self.data[_DEFAULT_SECTION])+len(self.data[section]))
 
 	return options, nil
 }
-

File config/read.go

 
 import (
 	"bufio"
+	"errors"
+	"io"
 	"os"
 	"strings"
 )
 
-
 // Base to read a file and get the configuration representation.
 // That representation can be queried with GetString, etc.
-func _read(fname string, c *Config) (*Config, os.Error) {
+func _read(fname string, c *Config) (*Config, error) {
 	file, err := os.Open(fname)
 	if err != nil {
 		return nil, err
 
 // ReadDefault reads a configuration file and returns its representation.
 // All arguments, except `fname`, are related to `New()`
-func Read(fname string, comment, separator string, preSpace, postSpace bool) (
-*Config, os.Error) {
+func Read(fname string, comment, separator string, preSpace, postSpace bool) (*Config, error) {
 	return _read(fname, New(comment, separator, preSpace, postSpace))
 }
 
 // ReadDefault reads a configuration file and returns its representation.
 // It uses values by default.
-func ReadDefault(fname string) (*Config, os.Error) {
+func ReadDefault(fname string) (*Config, error) {
 	return _read(fname, NewDefault())
 }
 
 // ===
 
-func (self *Config) read(buf *bufio.Reader) (err os.Error) {
+func (self *Config) read(buf *bufio.Reader) (err error) {
 	var section, option string
 
 	for {
 		l, err := buf.ReadString('\n') // parse line-by-line
-		if err == os.EOF {
+		if err == io.EOF {
 			break
 		} else if err != nil {
 			return err
 
 		// No new section and no section defined so
 		//case section == "":
-			//return os.NewError("no section defined")
+		//return os.NewError("no section defined")
 
 		// Other alternatives
 		default:
 				self.AddOption(section, option, prev+"\n"+value)
 
 			default:
-				return os.NewError("could not parse line: " + l)
+				return errors.New("could not parse line: " + l)
 			}
 		}
 	}
 	return nil
 }
-

File config/section.go

 
 package config
 
-
 // AddSection adds a new section to the configuration.
 //
 // If the section is nil then uses the section by default which it's already
 	}
 
 	for o, _ := range self.data[section] {
-		self.data[section][o] = nil, false // *value
+		delete(self.data[section], o) // *value
 	}
-	self.data[section] = nil, false
+	delete(self.data, section)
 
-	self.lastIdOption[section] = 0, false
-	self.idSection[section] = 0, false
+	delete(self.lastIdOption, section)
+	delete(self.idSection, section)
 
 	return true
 }
 
 	return sections
 }
-

File config/type.go

 package config
 
 import (
-	"os"
+	"errors"
 	"strconv"
 	"strings"
 )
 
-
 // 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 os.Error) {
+func (self *Config) Bool(section string, option string) (value bool, err error) {
 	sv, err := self.String(section, option)
 	if err != nil {
 		return false, err
 
 	value, ok := boolString[strings.ToLower(sv)]
 	if !ok {
-		return false, os.NewError("could not parse bool value: " + sv)
+		return false, errors.New("could not parse bool value: " + sv)
 	}
 
 	return value, nil
 }
 
 // Float has the same behaviour as String but converts the response to float.
-func (self *Config) Float(section string, option string) (value float64, err os.Error) {
+func (self *Config) Float(section string, option string) (value float64, err error) {
 	sv, err := self.String(section, option)
 	if err == nil {
 		value, err = strconv.Atof64(sv)
 }
 
 // Int has the same behaviour as String but converts the response to int.
-func (self *Config) Int(section string, option string) (value int, err os.Error) {
+func (self *Config) Int(section string, option string) (value int, err error) {
 	sv, err := self.String(section, option)
 	if err == nil {
 		value, err = strconv.Atoi(sv)
 // 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 os.Error) {
+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 {
 			return tValue.v, nil
 		}
-		return "", os.NewError(optionError(option).String())
+		return "", errors.New(optionError(option).String())
 	}
-	return "", os.NewError(sectionError(section).String())
+	return "", errors.New(sectionError(section).String())
 }
 
 // String gets the string value for the given option in the section.
 //
 // 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 os.Error) {
+func (self *Config) String(section string, option string) (value string, err error) {
 	value, err = self.RawString(section, option)
 	if err != nil {
 		return "", err
 			nvalue = self.data[section][noption]
 		}
 		if nvalue.v == "" {
-			return "", os.NewError(optionError(noption).String())
+			return "", errors.New(optionError(noption).String())
 		}
 
 		// substitute by new value and take off leading '%(' and trailing ')s'
 	}
 
 	if i == _DEPTH_VALUES {
-		return "", os.NewError("possible cycle while unfolding variables: " +
+		return "", errors.New("possible cycle while unfolding variables: " +
 			"max depth of " + strconv.Itoa(_DEPTH_VALUES) + " reached")
 	}
 
 	return value, nil
 }
-

File config/write.go

 	"strings"
 )
 
-
 // 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 uint32, header string) os.Error {
+func (self *Config) WriteFile(fname string, perm uint32, header string) error {
 	file, err := os.OpenFile(fname, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, perm)
 	if err != nil {
 		return err
 	return file.Close()
 }
 
-func (self *Config) write(buf *bufio.Writer, header string) (err os.Error) {
+func (self *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 {
 
 	return nil
 }
-