用自己的结构(解码的JSON)初始化外部库结构会产生类型错误

I have a struct, which is decoded from a JSON http POST req. My purpose of having this struct is: - Simplifying JSON request from client - Using the structs property data in another (external library) struct.

If i had to only use the external library struct, the client JSON setup will look confusing. How do i use my structs values inside another struct (and especially their arrays)?

I have a working solution for some of the values with simple types.

Consider the following: Ext lib struct:

type ExtStruct struct {
    From             *Email             
    Subject          string             
    Personalizations []*Personalization
 }

My lib struct:

type MyStruct struct {
         From            *Email             
         Subject          string             
         Personalizations []*Personalization
     }

This is my code as is:

myStruct := &MyStruct{}
err := json.NewDecoder(body).Decode(myStruct)
extStruct := &ExtStruct{
    Subject: myStruct.Subject,
    From:    (*extStruct.Email)(myStruct.From),
    Personalizations: []*extStruct.Personalization{
        To: ([]*extStruct.Email)(myStruct.To),
    }}

The Subjectand From value works, but i'm getting errors when trying to referencing array values. I fail to see where i am wrong. AFAIK there's no other option to "simplify" input JSON from a client, only to reference the values in another bigger struct. I can include the other referenced structs if needed, but think of it as:

type A struct {
    RefB []*B
}

type B struct {
    RefC []*C
    Value string
}
type C struct {
    Value string
}

And i need my struct to refer to C.

dpbtbcz6650
dpbtbcz6650 “JSON结构受ExtStruct约束”对我来说意义不大。客户端JSON和ExtStruct之间有一个映射,它可以是您想要的-没有理由使用MyStruct中介。参见例如play.golang.org/p/ma-3qcENokh,它使用惯用的方式创建映射。还有许多其他映射方式。
一年多之前 回复
doujuxin7392
doujuxin7392 没有伴侣,JSON结构受ExtStruct约束。从客户端的角度来看,这是我不满意的JSON结构。
一年多之前 回复
dongzhansong5785
dongzhansong5785 您是否对客户端JSON受ExtStruct约束有任何印象?它不是。JSON可以为{“Subject”:“Hello”},而ExtStruct可以很大,这没关系;所有其他非编组字段将设置为空值,但编组将成功。
一年多之前 回复
duanbo5230
duanbo5230 如果您在下面看到我的回答,则不会。简单构建的JSON输入被解码为MyStruct,而ExtStruct中使用的值。如我所说,我不知道是否有更好的解决方案,但这对我有用。
一年多之前 回复
dsb0003795
dsb0003795 但是JSON将是相同的,因此似乎要做的就是增加工作量和复杂性。
一年多之前 回复
douchi2022
douchi2022 您说得对,这最终使我感到困惑。关键是要从客户端创建不太混乱的JSON,然后在外部库中使用该数据,这需要更复杂的设置。
一年多之前 回复
duanlanzhi5509
duanlanzhi5509 ExtStruct和MyStruct是相同的,因此客户端JSON设置也将相同。这将如何减少混乱?
一年多之前 回复

2个回答

Maybe I misunderstood, but I would think the ExtStruct initialization would look more like

extStruct := &ExtStruct{
    From:             myStruct.From,
    Personalizations: myStruct.Personalizations,
}

https://play.golang.org/p/lqFFBa7SKGg

If they are incompatibly types, Email and Personalization, you should marshall into ExtStruct directly, or do marshalling between types yourself.

I think you should clarify your answer, maybe provide a working (but faulty) example.

douyin8809
douyin8809 我将尽快添加一些说明。 谢谢您的回答!
一年多之前 回复

Ok, so i found out, it's quite impossible to cast slices. If you refer to my post:

extStruct := &ExtStruct{
    Subject: myStruct.Subject,
    From:    (*extStruct.Email)(myStruct.From),
    Personalizations: []*extStruct.Personalization{
        To: ([]*extStruct.Email)(myStruct.To),
    }}

The Personalizations slice became:

Personalizations: ([]*extStruct.Personalization{{
    To: castStruct(myStruct.To),
}}),

And

func castStruct(input []*myStruct.Email) []*extStruct.Email {
    output := make([]*extStruct.Email, len(input))
    for index, content := range input {
        output[index] = (*extStruct.Email)(content)
    }
    return output
}

I don't know if there's a more clever way to do this, but this is how i succeeded eventually. Just comment this, if you want an elaboration, and I shall try to provide.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐