Comments (8)
I‘m sorry, I can't see your point. What's the meaning of from
? And why are you mentioning github.com/mitchellh/mapstructure?
You can reply me in Chinese.
from coven.
- from
type A struct {
D1 int64
}
type B struct {
D int64
}
A到B,有时候我们的struct 字段不一致。
- github.com/mitchellh/mapstructure 用于map[string]interface{} 转换到struct
因为go没有泛型,在解析json是能先转换成interface{}( 实际类型是map[string]interface ) 然后再转换成对应的struct
当然map to struct 可能不是你的初衷.
- 我目前在生产上使用这个包已经有一年,很nice!
这个包使用了比较底层的知识,我想扩充下结果无法下手~~
不知道有没有描述清楚。
from coven.
- I don't think it a good idea to add specific tag in struct, because it may cause conflict. But I think I can figure it out in another way.
- If you want to convert map to struct, you can first convert your map to map[string]interface, and then apply github.com/mitchellh/mapstructure.
from coven.
OK, thank you.
from coven.
type mapStructConverter struct {
*convertType
dFieldMap map[string]*reflect.StructField
sEmptyMapInterface *emptyInterface
}
func newMapStructConverter(convertType *convertType) (m converter) {
_, dFieldMap := extractFields(convertType.dstTyp, 0)
sEmpty := reflect.New(convertType.srcTyp).Interface()
m = &mapStructConverter{
convertType,
dFieldMap,
(*emptyInterface)(unsafe.Pointer(&sEmpty)),
}
return
}
// convert only affects destination map with keys that source map has, the rest will remain unchanged.
// dPtr and sPtr must pointed to a non-pointer value,
// it is assured by Converter.Convert() and elemConverter.convert()
func (m *mapStructConverter) convert(dPtr, sPtr unsafe.Pointer) {
sv := ptrToMapValue(m.sEmptyMapInterface, sPtr)
keys := sv.MapKeys()
for _, sKey := range keys {
if dValTyp, ok := m.dFieldMap[sKey.String()]; ok {
dVal := reflect.New(dValTyp.Type).Elem()
sValPtr := valuePtr(sv.MapIndex(sKey))
if elemConverter, ok := newElemConverter(dValTyp.Type, sv.MapIndex(sKey).Elem().Type()); ok {
elemConverter.convert(unsafe.Pointer(dVal.UnsafeAddr()), sValPtr)
}
}
}
}
func ExampleConverter_Convert2() {
type foobar struct {
D int
}
type Bar struct {
A []int
D int64
}
foo := map[string]interface{}{"A": []interface{}{1, 2, 3}, "D": 1}
var c = NewConverter(Bar{}, map[string]interface{}{})
bar := Bar{}
c.Convert(&bar, foo)
bytes, _ := json.Marshal(bar)
fmt.Println(string(bytes))
// Output:
// {"A":[1,2,3],"B":{"1":"ab","2":"ba","3":"cd"},"C":6,"D":11}
}
通过地址去操作 完全不明白,努力学习!!
from coven.
Hi, I have implemented StructOption
which can control struct converting details. I think it can meet your demand of from
. However, it doesn't suppose default
option, because I think it's not a responsibility of a convertor.
I also published a release of v1.0.0.
from coven.
coven
的核心**是先对类型进行分析并保存,在实际转换的时候可以直接通过指针操作来进行,这样可以加快转换的性能,并且避免了在转换过程中由于类型不匹配导致的错误。
如果你要将map[string]interface转换到struct,那么就不得不在转换过程中检查interface具体指向的类型,判断是否可以转换到struct的字段。这个过程跟coven
本身的流程是具有很大不一致的,在coven
中实现可能会花比较大的精力。因此我建议你具体参考 github.com/mitchellh/mapstructure 的实现方式。
from coven.
好的,谢谢你的热心解答!
from coven.
Related Issues (3)
- Error HOT 4
- struct 中的 time.time 没转换成功 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from coven.