Skip to content

Commit 1dd5628

Browse files
committed
[+] feat: feat: Add v.At(...).Set(...) methods and fix some spelling issues
1 parent 1d89554 commit 1dd5628

27 files changed

+261
-235
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[中文](./CHANGELOG_zh-cn.md)
44

5+
- [v1.4.1](#v141)
56
- [v1.4.0](#v140)
67
- [v1.3.8](#v138)
78
- [v1.3.7](#v137)
@@ -16,6 +17,10 @@
1617
- [v1.2.0](#v120)
1718
- [v1.1.1](#v111)
1819

20+
## v1.4.1
21+
22+
- Add `v.At(...).Set(...)` pattern, allowing putting keys ahead (comparing to `v.Set(...).At(...)`).
23+
1924
## v1.4.0
2025

2126
- Allow passing parameter with a single slice or array for Get, Set, Append, Insert, Delete methods.

CHANGELOG_zh-cn.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
[English](./CHANGELOG.md)
44

5+
- [v1.4.1](#v141)
56
- [v1.4.0](#v140)
67
- [v1.3.8](#v138)
78
- [v1.3.7](#v137)
@@ -16,6 +17,10 @@
1617
- [v1.2.0](#v120)
1718
- [v1.1.1](#v111)
1819

20+
## v1.4.1
21+
22+
- 新增 `v.At(...).Set(...)` 模式, 相比 `v.Set(...).At(...)` 模式不同的是将 key 放在前面
23+
1924
## v1.4.0
2025

2126
- 在 Get, Set, Append, Insert, Delete 等方法中, 允许传递一个切片参数, 用来标识 key 链

conv.go

+13-56
Original file line numberDiff line numberDiff line change
@@ -133,65 +133,22 @@ func escapeStringToBuff(s string, buf buffer.Buffer, opt *Opt) {
133133
}
134134
}
135135

136-
func intfToInt(v any) (u int, err error) {
137-
switch v := v.(type) {
138-
case int:
139-
u = v
140-
case uint:
141-
u = int(v)
142-
case int64:
143-
u = int(v)
144-
case uint64:
145-
u = int(v)
146-
case int32:
147-
u = int(v)
148-
case uint32:
149-
u = int(v)
150-
case int16:
151-
u = int(v)
152-
case uint16:
153-
u = int(v)
154-
case int8:
155-
u = int(v)
156-
case uint8:
157-
u = int(v)
136+
func anyToInt(v any) (u int, err error) {
137+
if v == nil {
138+
return 0, fmt.Errorf("%w: parameter is nil", ErrParameterError)
139+
}
140+
value := reflect.ValueOf(v)
141+
switch value.Kind() {
142+
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
143+
return int(value.Int()), nil
144+
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
145+
return int(value.Uint()), nil
158146
default:
159-
err = fmt.Errorf("%s is not a number", reflect.TypeOf(v).String())
147+
return 0, fmt.Errorf("%v is not a number", reflect.TypeOf(v))
160148
}
149+
}
161150

162-
return
163-
}
164-
165-
// func intfToInt64(v any) (i int64, err error) {
166-
// switch v.(type) {
167-
// case int:
168-
// i = int64(v.(int))
169-
// case uint:
170-
// i = int64(v.(uint))
171-
// case int64:
172-
// i = int64(v.(int64))
173-
// case uint64:
174-
// i = int64(v.(uint64))
175-
// case int32:
176-
// i = int64(v.(int32))
177-
// case uint32:
178-
// i = int64(v.(uint32))
179-
// case int16:
180-
// i = int64(v.(int16))
181-
// case uint16:
182-
// i = int64(v.(uint16))
183-
// case int8:
184-
// i = int64(v.(int8))
185-
// case uint8:
186-
// i = int64(v.(uint8))
187-
// default:
188-
// err = fmt.Errorf("%s is not a number", reflect.TypeOf(v).String())
189-
// }
190-
191-
// return
192-
// }
193-
194-
func intfToString(v any) (s string, err error) {
151+
func anyToString(v any) (s string, err error) {
195152
if v == nil {
196153
return "", fmt.Errorf("%w: parameter is nil", ErrParameterError)
197154
}

docs/en/03_set.md

+9
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,15 @@ This feature is so complicated that we will not use in most cases. But there is
119119
fmt.Println(c.MustMarshalString())
120120
```
121121

122+
If you like placing keys ahead, you can use the `v.At(...).Set(...)` pattern:
123+
124+
```go
125+
// ...
126+
v.At("array", i, "word").Set(words[i])
127+
v.At("array", i, "lesson").Set(lessons[i]).
128+
// ...
129+
```
130+
122131
Final output:
123132

124133
```json

docs/zh-cn/03_set.md

+9
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,15 @@ v.MustSet("Hello, array!").At("arr", 0) // {"obj":{"message":"Hello, ob
115115
fmt.Println(c.MustMarshalString())
116116
```
117117

118+
如果你喜欢把 key 放在前面,那你可以使用 `v.At(...).Set(...)` 模式:
119+
120+
```go
121+
// ...
122+
v.At("array", i, "word").Set(words[i])
123+
v.At("array", i, "lesson").Set(lessons[i]).
124+
// ...
125+
```
126+
118127
最终输出为:
119128

120129
```json

errors.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package jsonvalue
22

3-
// Error is equavilent to string and used to create some error constants in this package.
3+
// Error is equivalent to string and used to create some error constants in this package.
44
// Error constants: http://godoc.org/github.com/Andrew-M-C/go.jsonvalue/#pkg-constants
55
type Error string
66

@@ -9,7 +9,7 @@ func (e Error) Error() string {
99
}
1010

1111
const (
12-
// ErrNilParameter identifies input paremeter is nil
12+
// ErrNilParameter identifies input parameter is nil
1313
//
1414
// ErrNilParameter 表示参数为空
1515
ErrNilParameter = Error("nil parameter")
@@ -19,10 +19,10 @@ const (
1919
// ErrValueUninitialized 表示当前的 jsonvalue 实例未初始化
2020
ErrValueUninitialized = Error("jsonvalue instance is not initialized")
2121

22-
// ErrRawBytesUnrecignized identifies all unexpected raw bytes
22+
// ErrRawBytesUnrecognized identifies all unexpected raw bytes
2323
//
24-
// ErrRawBytesUnrecignized 表示无法识别的序列文本
25-
ErrRawBytesUnrecignized = Error("unrecognized raw text")
24+
// ErrRawBytesUnrecognized 表示无法识别的序列文本
25+
ErrRawBytesUnrecognized = Error("unrecognized raw text")
2626

2727
// ErrNotValidNumberValue shows that a value starts with number or '-' is not eventually a number value
2828
//
@@ -34,10 +34,10 @@ const (
3434
// ErrNotValidBoolValue 表示当前值不是一个合法的布尔值
3535
ErrNotValidBoolValue = Error("not a valid bool value")
3636

37-
// ErrNotValidNulllValue shows that a value starts with 'n' is not eventually a bool value
37+
// ErrNotValidNullValue shows that a value starts with 'n' is not eventually a bool value
3838
//
39-
// ErrNotValidNulllValue 表示当前不是一个 null 值类型的 JSON
40-
ErrNotValidNulllValue = Error("not a valid null value")
39+
// ErrNotValidNullValue 表示当前不是一个 null 值类型的 JSON
40+
ErrNotValidNullValue = Error("not a valid null value")
4141

4242
// ErrOutOfRange identifies that given position for a JSON array is out of range
4343
//
@@ -64,7 +64,7 @@ const (
6464
// ErrNotArrayValue 表示当前不是一个数组类型 JSON
6565
ErrNotArrayValue = Error("not an array typed value")
6666

67-
// ErrNotObjectValue shows that operation target value is not an valie object
67+
// ErrNotObjectValue shows that operation target value is not an valid object
6868
//
6969
// ErrNotObjectValue 表示当前不是一个合法的对象类型 JSON
7070
ErrNotObjectValue = Error("not an object typed value")

0 commit comments

Comments
 (0)