Skip to content

Commit d890071

Browse files
authored
[type:feat] support properties (#102)
* [type:feat] support properties
1 parent bc63af1 commit d890071

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed

core/idutil.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package core
219

320
import (

core/idutil_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
118
package core
219

320
import (

core/props.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
id=1
2+
name="xiaoming"

core/propsutil.go

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package core
19+
20+
import (
21+
"bufio"
22+
"github.com/acmestack/godkits/gox/stringsx"
23+
"io"
24+
"os"
25+
"strings"
26+
)
27+
28+
type Properties struct {
29+
m map[string]string
30+
}
31+
32+
func NewProperties() *Properties {
33+
return &Properties{
34+
m: map[string]string{},
35+
}
36+
}
37+
38+
// GetConfigFromFile get map from file
39+
// @param path file path
40+
// @return map[string]string
41+
// usage:
42+
// p := NewProperties()
43+
// p.GetConfigFromFile(your path)
44+
func (p *Properties) GetConfigFromFile(path string) (err error) {
45+
f, err := os.Open(path)
46+
defer f.Close()
47+
if err != nil {
48+
panic(err)
49+
}
50+
51+
r := bufio.NewReader(f)
52+
for {
53+
b, _, err := r.ReadLine()
54+
if err != nil {
55+
if err == io.EOF {
56+
break
57+
}
58+
panic(err)
59+
}
60+
s := strings.TrimSpace(string(b))
61+
index := strings.Index(s, "=")
62+
if index < 0 {
63+
continue
64+
}
65+
key := strings.TrimSpace(s[:index])
66+
if len(key) == 0 {
67+
continue
68+
}
69+
value := strings.TrimSpace(s[index+1:])
70+
if len(value) == 0 {
71+
continue
72+
}
73+
_, _, err = p.put(key, value)
74+
}
75+
return nil
76+
}
77+
78+
// Get load value by key from Properties
79+
// @receiver p properties
80+
// @param key the key
81+
// @return value the value
82+
// @return ok status
83+
func (p *Properties) Get(key string) (value string, ok bool) {
84+
_, ok = p.m[key]
85+
if !ok {
86+
return "", false
87+
}
88+
return p.m[key], true
89+
}
90+
91+
// put put key,value into properties
92+
// @receiver p properties
93+
// @param key key
94+
// @param value value
95+
// @return preValue pre
96+
// @return ok status
97+
// @return err error
98+
func (p *Properties) put(key, value string) (preValue string, ok bool, err error) {
99+
if stringsx.Empty(key) {
100+
return "", false, nil
101+
}
102+
preValue, ok = p.Get(key)
103+
// check key and value
104+
if !ok {
105+
p.m[key] = value
106+
return preValue, true, nil
107+
}
108+
panic("key repeat!")
109+
}

core/propsutils_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2022, AcmeStack
3+
* All rights reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package core
19+
20+
import (
21+
"github.com/acmestack/godkits/assert"
22+
"testing"
23+
)
24+
25+
func TestGetConfigFromFile(t *testing.T) {
26+
p := NewProperties()
27+
_ = p.GetConfigFromFile("props.properties")
28+
val, ok := p.Get("id")
29+
assert.IsTrue(t, ok)
30+
assert.Equal(t, 1, val)
31+
val2, ok2 := p.Get("name")
32+
assert.IsTrue(t, ok2)
33+
assert.Equal(t, "xiaoming", val2)
34+
val3, ok3 := p.Get("mm")
35+
assert.IsFalse(t, ok3)
36+
assert.IsTrue(t, val3 == "")
37+
}

0 commit comments

Comments
 (0)