Permalink
Browse files

Radical change. Move towards goinstall compatibility.

- Remove goconfig module, instead use json-based config files.
- Don't build goremote and don't generate rpc.go each time.
- Fix small issue when `gocode set lib-path ""` was unacceptable.
  • Loading branch information...
1 parent 82c9044 commit a889242167e791026dbadbe92342cbffbbcdc567 @nsf committed Dec 26, 2011
Showing with 180 additions and 795 deletions.
  1. +0 −1 .gitignore
  2. +0 −17 Makefile
  3. +40 −72 config.go
  4. +2 −2 gocode.go
  5. +0 −7 goconfig/Makefile
  6. +0 −16 goconfig/README
  7. +0 −462 goconfig/configfile.go
  8. +0 −216 goconfig/configfile_test.go
  9. +136 −0 rpc.go
  10. +2 −2 server.go
View
@@ -4,7 +4,6 @@
gocode
goremote
gocodetest
-rpc.go
*.swp
listidents
showcursor
View
@@ -1,6 +1,5 @@
include $(GOROOT)/src/Make.inc
-PREREQ+=configfile.a
TARG=gocode
GOFILES=gocode.go\
autocompletefile.go\
@@ -22,19 +21,3 @@ GOFILES+=os_posix.go
endif
include $(GOROOT)/src/Make.cmd
-
-rpc.go: server.go goremote/goremote
- ./goremote/goremote server.go | gofmt > rpc.go
-
-goremote/goremote: goremote/goremote.go
- gomake -C goremote
-
-configfile.a: goconfig/configfile.go
- gomake -C goconfig
- cp goconfig/_obj/configfile.a .
-
-clean: cleandeps
-
-cleandeps:
- gomake -C goremote clean
- gomake -C goconfig clean
View
112 config.go
@@ -1,15 +1,15 @@
package main
import (
- cfg "./configfile"
"bytes"
- "errors"
"fmt"
"io"
+ "io/ioutil"
"os"
"path/filepath"
"reflect"
"strconv"
+ "encoding/json"
)
//-------------------------------------------------------------------------
@@ -20,17 +20,32 @@ import (
//-------------------------------------------------------------------------
var Config = struct {
- ProposeBuiltins bool "propose-builtins"
- LibPath string "lib-path"
+ ProposeBuiltins bool `json:"propose-builtins"`
+ LibPath string `json:"lib-path"`
}{
false,
"",
}
+var boolStrings = map[string]bool{
+ "t": true,
+ "true": true,
+ "y": true,
+ "yes": true,
+ "on": true,
+ "1": true,
+ "f": false,
+ "false": false,
+ "n": false,
+ "no": false,
+ "off": false,
+ "0": false,
+}
+
func setValue(v reflect.Value, name, value string) {
switch t := v; t.Kind() {
case reflect.Bool:
- v, ok := cfg.BoolStrings[value]
+ v, ok := boolStrings[value]
if ok {
t.SetBool(v)
}
@@ -71,8 +86,8 @@ func listConfig(v interface{}) string {
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
- name := typ.Field(i).Tag
- listValue(v, string(name), buf)
+ name := typ.Field(i).Tag.Get("json")
+ listValue(v, name, buf)
}
return buf.String()
}
@@ -86,8 +101,8 @@ func listOption(v interface{}, name string) string {
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
- nm := typ.Field(i).Tag
- if string(nm) == name {
+ nm := typ.Field(i).Tag.Get("json")
+ if nm == name {
listValue(v, name, buf)
}
}
@@ -103,8 +118,8 @@ func setOption(v interface{}, name, value string) string {
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := 0; i < str.NumField(); i++ {
v := str.Field(i)
- nm := typ.Field(i).Tag
- if string(nm) == name {
+ nm := typ.Field(i).Tag.Get("json")
+ if nm == name {
setValue(v, name, value)
listValue(v, name, buf)
}
@@ -128,59 +143,20 @@ func interfaceIsPtrStruct(v interface{}) (reflect.Value, reflect.Type, bool) {
return str, typ, true
}
-func writeValue(v reflect.Value, name string, c *cfg.ConfigFile) {
- switch v.Kind() {
- case reflect.Bool, reflect.String,
- reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Float32, reflect.Float64:
- c.AddOption(cfg.DefaultSection, name, fmt.Sprint(v.Interface()))
- }
-}
-
-func readValue(v reflect.Value, name string, c *cfg.ConfigFile) {
- if !c.HasOption(cfg.DefaultSection, name) {
- return
- }
- switch t := v; t.Kind() {
- case reflect.Bool:
- v, err := c.GetBool(cfg.DefaultSection, name)
- if err == nil {
- t.SetBool(v)
- }
- case reflect.String:
- v, err := c.GetString(cfg.DefaultSection, name)
- if err == nil {
- t.SetString(v)
- }
- case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
- v, err := c.GetInt(cfg.DefaultSection, name)
- if err == nil {
- t.SetInt(int64(v))
- }
- case reflect.Float32, reflect.Float64:
- v, err := c.GetFloat(cfg.DefaultSection, name)
- if err == nil {
- t.SetFloat(float64(v))
- }
- }
-}
-
func writeConfig(v interface{}) error {
- const errstr = "WriteConfig expects a pointer to a struct value as an argument"
-
- str, typ, ok := interfaceIsPtrStruct(v)
- if !ok {
- return errors.New(errstr)
+ data, err := json.Marshal(v)
+ if err != nil {
+ return err
}
- c := cfg.NewConfigFile()
- for i := 0; i < str.NumField(); i++ {
- v := str.Field(i)
- name := typ.Field(i).Tag
- writeValue(v, string(name), c)
+ makeSureConfigDirExists()
+ f, err := os.Create(configFile())
+ if err != nil {
+ return err
}
+ defer f.Close()
- makeSureConfigDirExists()
- err := c.WriteConfigFile(configFile(), 0644, "gocode config file")
+ _, err = f.Write(data)
if err != nil {
return err
}
@@ -189,22 +165,14 @@ func writeConfig(v interface{}) error {
}
func readConfig(v interface{}) error {
- c, err := cfg.ReadConfigFile(configFile())
+ data, err := ioutil.ReadFile(configFile())
if err != nil {
return err
}
- const errstr = "ReadConfig expects a pointer to a struct value as an argument"
-
- str, typ, ok := interfaceIsPtrStruct(v)
- if !ok {
- return errors.New(errstr)
- }
-
- for i := 0; i < str.NumField(); i++ {
- v := str.Field(i)
- name := typ.Field(i).Tag
- readValue(v, string(name), c)
+ err = json.Unmarshal(data, v)
+ if err != nil {
+ return err
}
return nil
@@ -226,5 +194,5 @@ func makeSureConfigDirExists() {
}
func configFile() string {
- return filepath.Join(xdgHomeDir(), "gocode", "config.ini")
+ return filepath.Join(xdgHomeDir(), "gocode", "config.json")
}
View
@@ -279,9 +279,9 @@ func cmdDropCache(c *rpc.Client) {
func cmdSet(c *rpc.Client) {
switch flag.NArg() {
case 1:
- fmt.Print(Client_Set(c, "", ""))
+ fmt.Print(Client_Set(c, "\x00", "\x00"))
case 2:
- fmt.Print(Client_Set(c, flag.Arg(1), ""))
+ fmt.Print(Client_Set(c, flag.Arg(1), "\x00"))
case 3:
fmt.Print(Client_Set(c, flag.Arg(1), flag.Arg(2)))
}
View
@@ -1,7 +0,0 @@
-include $(GOROOT)/src/Make.inc
-
-TARG=configfile
-GOFILES=\
- configfile.go\
-
-include $(GOROOT)/src/Make.pkg
View
@@ -1,16 +0,0 @@
-The Makefile and configfile_test.py are to be used only
-if you want to place this package in the standard library.
-
-To place this package in the standard library and run
-the test cases do:
-mkdir $GOROOT/src/pkg/configfile/
-cp configfile.go configfile_test.go Makefile $GOROOT/src/pkg/configfile/
-cd $GOROOT/src/pkg/configfile/
-make
-gotest configfile_test.go
-(and you should see PASS).
-
-Otherwise, to use it separately from the standard library
-just do as usual:
-6g configfile.go
-... and then link to your own source file.
Oops, something went wrong.

0 comments on commit a889242

Please sign in to comment.