Skip to content

Commit 0a6d002

Browse files
committed
Merge pull request #13 from qiniu/develop
Release v7.0.6 - qiniupkg.com/x/{config.v7, rpc.v7/gob}
2 parents 83501c5 + b2e9bfd commit 0a6d002

File tree

5 files changed

+325
-0
lines changed

5 files changed

+325
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@ go get qiniupkg.com/x
1212
# 使用文档
1313

1414
* [qiniupkg.com/x/bytes.v7](http://godoc.org/qiniupkg.com/x/bytes.v7)
15+
* [qiniupkg.com/x/config.v7](http://godoc.org/qiniupkg.com/x/config.v7)
1516
* [qiniupkg.com/x/ctype.v7](http://godoc.org/qiniupkg.com/x/ctype.v7)
1617
* [qiniupkg.com/x/log.v7](http://godoc.org/qiniupkg.com/x/log.v7)
1718
* [qiniupkg.com/x/reqid.v7](http://godoc.org/qiniupkg.com/x/reqid.v7)
1819
* [qiniupkg.com/x/rpc.v7](http://godoc.org/qiniupkg.com/x/rpc.v7)
20+
* [qiniupkg.com/x/rpc.v7/gob](http://godoc.org/qiniupkg.com/x/rpc.v7/gob)
1921
* [qiniupkg.com/x/url.v7](http://godoc.org/qiniupkg.com/x/url.v7)
2022
* [qiniupkg.com/x/xlog.v7](http://godoc.org/qiniupkg.com/x/xlog.v7)

config.v7/getdir.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package config
2+
3+
import (
4+
"errors"
5+
"os"
6+
)
7+
8+
var homeEnvNames = [][]string{
9+
{"HOME"},
10+
{"HOMEDRIVE", "HOMEPATH"},
11+
}
12+
13+
var (
14+
ErrHomeNotFound = errors.New("$HOME not found")
15+
)
16+
17+
func getEnv(name []string) (v string) {
18+
19+
if len(name) == 1 {
20+
return os.Getenv(name[0])
21+
}
22+
for _, k := range name {
23+
v += os.Getenv(k)
24+
}
25+
return
26+
}
27+
28+
func GetDir(app string) (dir string, err error) {
29+
30+
for _, name := range homeEnvNames {
31+
home := getEnv(name)
32+
if home == "" {
33+
continue
34+
}
35+
dir = home + "/." + app
36+
err = os.MkdirAll(dir, 0777)
37+
return
38+
}
39+
return "", ErrHomeNotFound
40+
}
41+

config.v7/load_conf.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
package config
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"flag"
7+
"io/ioutil"
8+
9+
"qiniupkg.com/x/log.v7"
10+
)
11+
12+
var (
13+
confName *string
14+
)
15+
16+
func Init(cflag, app, default_conf string) {
17+
18+
confDir, _ := GetDir(app)
19+
confName = flag.String(cflag, confDir+"/"+default_conf, "the config file")
20+
}
21+
22+
func GetPath() string {
23+
24+
if confName != nil {
25+
return *confName
26+
}
27+
return ""
28+
}
29+
30+
func Load(conf interface{}) (err error) {
31+
32+
if !flag.Parsed() {
33+
flag.Parse()
34+
}
35+
36+
log.Info("Use the config file of ", *confName)
37+
return LoadEx(conf, *confName)
38+
}
39+
40+
func LoadEx(conf interface{}, confName string) (err error) {
41+
42+
data, err := ioutil.ReadFile(confName)
43+
if err != nil {
44+
log.Error("Load conf failed:", err)
45+
return
46+
}
47+
data = trimComments(data)
48+
49+
err = json.Unmarshal(data, conf)
50+
if err != nil {
51+
log.Error("Parse conf failed:", err)
52+
}
53+
return
54+
}
55+
56+
func LoadFile(conf interface{}, confName string) (err error) {
57+
58+
data, err := ioutil.ReadFile(confName)
59+
if err != nil {
60+
return
61+
}
62+
data = trimComments(data)
63+
64+
return json.Unmarshal(data, conf)
65+
}
66+
67+
func LoadBytes(conf interface{}, data []byte) (err error) {
68+
69+
return json.Unmarshal(trimComments(data), conf)
70+
}
71+
72+
func LoadString(conf interface{}, data string) (err error) {
73+
74+
return json.Unmarshal(trimComments([]byte(data)), conf)
75+
}
76+
77+
func trimComments(data []byte) (data1 []byte) {
78+
79+
var line []byte
80+
81+
data1 = data[:0]
82+
for {
83+
pos := bytes.IndexByte(data, '\n')
84+
if pos < 0 {
85+
line = data
86+
} else {
87+
line = data[:pos+1]
88+
}
89+
data1 = append(data1, trimCommentsLine(line)...)
90+
if pos < 0 {
91+
return
92+
}
93+
data = data[pos+1:]
94+
}
95+
}
96+
97+
func trimCommentsLine(line []byte) []byte {
98+
99+
n := len(line)
100+
quoteCount := 0
101+
for i := 0; i < n; i++ {
102+
c := line[i]
103+
switch c {
104+
case '\\':
105+
i++
106+
case '"':
107+
quoteCount++
108+
case '#':
109+
if (quoteCount&1) == 0 {
110+
return line[:i]
111+
}
112+
}
113+
}
114+
return line
115+
}
116+

config.v7/load_conf_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package config_test
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"testing"
7+
8+
"qiniupkg.com/x/config.v7"
9+
)
10+
11+
func TestTrimComments(t *testing.T) {
12+
13+
confData := `{
14+
"debug_level": 0, # 调试级别
15+
"rs_host": "http://localhost:15001", #RS服务
16+
"limit": 5, #限制数
17+
"retryTimes": 56,
18+
"quote0": "###",
19+
"quote": "quo\\\"\\#",
20+
"ant": "ant\\#" #123
21+
}`
22+
23+
confDataExp := `{
24+
"debug_level": 0,
25+
"rs_host": "http://localhost:15001",
26+
"limit": 5,
27+
"retryTimes": 56,
28+
"quote0": "###",
29+
"quote": "quo\\\"\\#",
30+
"ant": "ant\\#"
31+
}`
32+
33+
var (
34+
conf, confExp interface{}
35+
)
36+
err := config.LoadString(&conf, confData)
37+
if err != nil {
38+
t.Fatal("config.LoadString(conf) failed:", err)
39+
}
40+
err = config.LoadString(&confExp, confDataExp)
41+
if err != nil {
42+
t.Fatal("config.LoadString(confExp) failed:", err)
43+
}
44+
45+
b, err := json.Marshal(conf)
46+
if err != nil {
47+
t.Fatal("json.Marshal failed:", err)
48+
}
49+
50+
bExp, err := json.Marshal(confExp)
51+
if err != nil {
52+
t.Fatal("json.Marshal(exp) failed:", err)
53+
}
54+
55+
if !bytes.Equal(b, bExp) {
56+
t.Fatal("b != bExp")
57+
}
58+
}
59+

rpc.v7/gob/gobrpc_client.go

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package gob
2+
3+
import (
4+
"bytes"
5+
"encoding/gob"
6+
"io"
7+
"io/ioutil"
8+
"net/http"
9+
"strconv"
10+
11+
"qiniupkg.com/x/rpc.v7"
12+
13+
. "golang.org/x/net/context"
14+
)
15+
16+
// ---------------------------------------------------------------------------
17+
18+
func Register(value interface{}) {
19+
20+
gob.Register(value)
21+
}
22+
23+
func RegisterName(name string, value interface{}) {
24+
25+
gob.RegisterName(name, value)
26+
}
27+
28+
// ---------------------------------------------------------------------------
29+
30+
func ResponseError(resp *http.Response) (err error) {
31+
32+
e := &rpc.ErrorInfo{
33+
Reqid: resp.Header.Get("X-Reqid"),
34+
Code: resp.StatusCode,
35+
}
36+
if resp.StatusCode > 299 {
37+
e.Err = resp.Header.Get("X-Err")
38+
if errno := resp.Header.Get("X-Errno"); errno != "" {
39+
v, err2 := strconv.ParseInt(errno, 10, 32)
40+
if err2 != nil {
41+
e.Err = err2.Error()
42+
}
43+
e.Errno = int(v)
44+
}
45+
}
46+
return e
47+
}
48+
49+
func CallRet(ctx Context, ret interface{}, resp *http.Response) (err error) {
50+
51+
defer func() {
52+
io.Copy(ioutil.Discard, resp.Body)
53+
resp.Body.Close()
54+
}()
55+
56+
if resp.StatusCode/100 == 2 {
57+
if ret != nil && resp.ContentLength != 0 {
58+
err = gob.NewDecoder(resp.Body).Decode(ret)
59+
if err != nil {
60+
return
61+
}
62+
}
63+
if resp.StatusCode == 200 {
64+
return nil
65+
}
66+
}
67+
return ResponseError(resp)
68+
}
69+
70+
// ---------------------------------------------------------------------------
71+
72+
type Client struct {
73+
rpc.Client
74+
}
75+
76+
var (
77+
DefaultClient = Client{rpc.DefaultClient}
78+
)
79+
80+
func (r Client) Call(
81+
ctx Context, ret interface{}, method, url1 string) (err error) {
82+
83+
resp, err := r.DoRequestWith(ctx, method, url1, "application/gob", nil, 0)
84+
if err != nil {
85+
return err
86+
}
87+
return CallRet(ctx, ret, resp)
88+
}
89+
90+
func (r Client) CallWithGob(
91+
ctx Context, ret interface{}, method, url1 string, params interface{}) (err error) {
92+
93+
var b bytes.Buffer
94+
err = gob.NewEncoder(&b).Encode(params)
95+
if err != nil {
96+
return err
97+
}
98+
99+
resp, err := r.DoRequestWith(ctx, method, url1, "application/gob", &b, b.Len())
100+
if err != nil {
101+
return err
102+
}
103+
return CallRet(ctx, ret, resp)
104+
}
105+
106+
// ---------------------------------------------------------------------------
107+

0 commit comments

Comments
 (0)