在Golang中解析一个简单的YAML文件,需要找出正确的结构体。

huangapple go评论75阅读模式
英文:

Figuring out the right struct to parse a simple YAML file in Golang

问题

我有一个相当简单的YAML文档需要解析成Go中的(最好是)映射。

YAML文档:

---
A: Logon
'0': Heartbeat
'1': Test Request
'2': Resend Request
'3': Reject
'4': Sequence Reset
'5': Logout
'8': Execution Report
S: Quote
AE: Trade Capture Report
B: News
h: Trading Session Status
f: Security Status

我尝试使用以下代码进行编组:

type TranslationVal struct {
    Map map[string]string
}

translationVal := TranslationVal{}
err := yaml.Unmarshal([]byte(val), &translationVal)

然而,我得到了以下错误:

2017/08/22 20:33:23 yaml: unmarshal errors:   line 1: cannot unmarshal !!str `A` into main.TranslationVal
英文:

I have a fairly simple YAML document to parse into a (preferably) map in Go.

YAML doc:

---
A: Logon
'0': Heartbeat
'1': Test Request
'2': Resend Request
'3': Reject
'4': Sequence Reset
'5': Logout
'8': Execution Report
S: Quote
AE: Trade Capture Report
B: News
h: Trading Session Status
f: Security Status

I'm trying to marshal it with

type TranslationVal struct {
	Map map[string]string
}

translationVal := TranslationVal{}
err := yaml.Unmarshal([]byte(val), &translationVal)

However I'm getting:

2017/08/22 20:33:23 yaml: unmarshal errors:   line 1: cannot unmarshal !!str `A` into main.TranslationVal

答案1

得分: 1

问题是由于您将地图包装在对象中引起的,YAML 没有这样的嵌套。

您实际上可以直接将其解组到地图本身中

编辑:很难通过您的格式确定,但如果那些整数键嵌套在 A 下面,那么您还需要一个不同的结构,它实际上将是一个 map[string]map[string]string -- 但是那样相当丑陋,所以我建议在那一点上转向不同的范例... 您可以使用 map[string]interface{},它不会关心哪些类型进入地图,然后稍后再处理它,或者您可以更静态地定义对象,使用实际的键,例如 A 在结构中表示每个项目的位置,如果是这种情况,您将拥有以下对象;

type TranslationVal struct {
A map[string]string
B string
C string
// and so on
F string yaml:f // necessary because f would be unexported
}

英文:

The issue is caused by you wrapping the map in an object, the YAML has no such nesting.

map := map[string]string{}
err := yaml.Unmarshal([]byte(val), &map)

You can actually just unmarshal directly into the map itself

EDIT: hard to tell with your formatting but if those integer keys are nested under A then you will need a different structure as well, it would actually be a map[string]map[string]string -- however that is rather ugly so I would recommend moving to a different paradigm at that point... You could either use a map[string]interface{} which wouldn't care what types go into the map and then you could deal with it later or you could define the object more statically, using actually keys such a A in a struct to denote where each item goes, if that were the case you'd have an object like the following;

type TranslationVal struct {
    A map[string]string
    B string
    C string
    // and so on
    F string `yaml:f` // necessary because f would be unexported
}

答案2

得分: -1

我实际上尝试解析一个根本不是 YAML 的值,哎呀!

英文:

I actually tried to parse a value that wasn't YAML at all, doh!!

huangapple
  • 本文由 发表于 2017年8月23日 01:52:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/45824062.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定