how to escape ! while unmarshalling the yamlfile in golang

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

how to escape ! while unmarshalling the yamlfile in golang

问题

我有一个包含特殊字符!的键值对的yaml文件,如下所示:

name: Amit Kumar
age: !kk
address:
  street: !123 Some Random Street
  city: Chennai
  state: TN
  zip: 763098
phoneNumbers:
  - type: home
    number: 0123456789
  - type: work
    number: 0987654321

在解析这个yaml文件时,以!开头的任何单词都会被移除,例如street: !123 Some Random Streetage: !dd

例如:
person.ageperson.address将会得到以下结果:

   age: kk 
   address: map[city:Chennai state:TN street:Some Random Street zip:763098]

期望的结果是:

   age: !kk 
   address: map[city:Chennai state:TN street:!123 Some Random Street zip:763098]

有关在解析yaml文件时保留特殊字符的建议吗?

Go文件:

package main

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
)

type Address struct {
	Street string `yaml:"street"`
	City   string `yaml:"city"`
	State  string `yaml:"state"`
	Zip    string `yaml:"zip"`
}

type PhoneNumber struct {
	Type   string `yaml:"type"`
	Number string `yaml:"number"`
}

type Person struct {
	Name        string        `yaml:"name"`
	Age         string        `yaml:"age"`
	Address     interface{}   `yaml:"address"`
	PhoneNumber []PhoneNumber `yaml:"phoneNumbers"`
}

func main() {
	// 读取文件
	data, err := ioutil.ReadFile("testyamltemplate.yaml")
	if err != nil {
		fmt.Println(err)
		return
	}

	// 创建一个结构体来保存YAML数据
	var person Person

	// 将YAML数据解析到结构体中
	err = yaml.Unmarshal(data, &person)
	if err != nil {
		fmt.Println(err)
		return
	}

	// 打印数据
	fmt.Println(person.Age)
}

以上是你要翻译的内容。

英文:

I have yaml file which contain special character ! in the key-value pair as shown below:

name: Amit Kumar
age: !kk
address:
  street: !123 Some Random Street
  city: Chennai
  state: TN
  zip: 763098
phoneNumbers:
  - type: home
    number: 0123456789
  - type: work
    number: 0987654321

While unmarshall this yaml file, street: !123 Some Random Street or age: !dd, any word that begin with ! are removed.
For example:
person.age or person.address will result in

   age: kk 
   address: map[city:Chennai state:TN street:Some Random Street zip:763098]

expected:

   age: !kk 
   address: map[city:Chennai state:TN street:!123 Some Random Street zip:763098]

Any suggestion on retaining the special character while unmarshalling the yaml file?

Go file:

package main

import (
	"fmt"
	"gopkg.in/yaml.v3"
	"io/ioutil"
)

type Address struct {
	Street string `yaml:"street"`
	City   string `yaml:"city"`
	State  string `yaml:"state"`
	Zip    string `yaml:"zip"`
}

type PhoneNumber struct {
	Type   string `yaml:"type"`
	Number string `yaml:"number"`
}

type Person struct {
	Name        string        `yaml:"name"`
	Age         string        `yaml:"age"`
	Address     interface{}   `yaml:"address"`
	PhoneNumber []PhoneNumber `yaml:"phoneNumbers"`
}

func main() {
	// Read the file
	data, err := ioutil.ReadFile("testyamltemplate.yaml")
	if err != nil {
		fmt.Println(err)
		return
	}

	// Create a struct to hold the YAML data
	var person Person

	// Unmarshal the YAML data into the struct
	err = yaml.Unmarshal(data, &person)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Print the data
	fmt.Println(person.Age)
}

答案1

得分: 2

未引用的前导感叹号在YAML规范中被视为标签的开始。

为了防止这种解释,可以用双引号或单引号将整个值包裹起来:

age: "!kk"
street: "!123 Some Random Street"
英文:

Unquoted leading exclamation mark is treated by YAML specification as a start of a tag.

For prevent such interpretation wrap the whole value with double or single quotes:

age: "!kk"
street: "!123 Some Random Street"

huangapple
  • 本文由 发表于 2023年2月4日 07:36:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/75341983.html
匿名

发表评论

匿名网友

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

确定