Skip to content

Instantly share code, notes, and snippets.

@goFrendiAsgard
Created July 20, 2021 03:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goFrendiAsgard/d321f971520eda5a50a3c1301372e38c to your computer and use it in GitHub Desktop.
Save goFrendiAsgard/d321f971520eda5a50a3c1301372e38c to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"gopkg.in/yaml.v3"
)
var data string = `
# comment
age: 15
name: Gita Hartanto
#this is the hobbies
hobbies:
- eat
- sleep
# yo, another comment
- drink
properties:
motto: Anda pasti bisa bangkrut
car: AG1A
`
func main() {
var x yaml.Node
err := yaml.Unmarshal([]byte(data), &x)
if err != nil {
panic(err)
}
// analyze
for index := 0; index < len(x.Content[0].Content); index += 1 {
fmt.Println(index, x.Content[0].Content[index].Value, x.Content[0].Content[index].ShortTag())
}
fmt.Println("===")
// set name to "Tobing ora umum"
for index := 0; index < len(x.Content[0].Content); index += 2 {
if x.Content[0].Content[index].ShortTag() == "!!str" && x.Content[0].Content[index].Value == "name" {
x.Content[0].Content[index+1].SetString("Gita Hartanto alias Tobing")
}
}
// translate hobbies to bahasa Indonesia
for index := 0; index < len(x.Content[0].Content); index += 2 {
if x.Content[0].Content[index].ShortTag() == "!!str" && x.Content[0].Content[index+1].ShortTag() == "!!seq" && x.Content[0].Content[index].Value == "hobbies" {
for hobbyIndex := 0; hobbyIndex < len(x.Content[0].Content[index+1].Content); hobbyIndex++ {
hobbyVal := x.Content[0].Content[index+1].Content[hobbyIndex].Value
newHobbyVal := hobbyVal
switch hobbyVal {
case "eat":
newHobbyVal = "makan"
case "sleep":
newHobbyVal = "tidur"
case "drink":
newHobbyVal = "minum"
}
x.Content[0].Content[index+1].Content[hobbyIndex].SetString(newHobbyVal)
}
}
}
// add new hobby
for index := 0; index < len(x.Content[0].Content); index += 2 {
if x.Content[0].Content[index].ShortTag() == "!!str" && x.Content[0].Content[index+1].ShortTag() == "!!seq" && x.Content[0].Content[index].Value == "hobbies" {
var newHobby yaml.Node
yaml.Unmarshal([]byte("pitching qnet"), &newHobby)
x.Content[0].Content[index+1].Content = append(x.Content[0].Content[index+1].Content, newHobby.Content[0])
}
}
// change car
for index := 0; index < len(x.Content[0].Content); index += 2 {
if x.Content[0].Content[index].ShortTag() == "!!str" && x.Content[0].Content[index+1].ShortTag() == "!!map" && x.Content[0].Content[index].Value == "properties" {
for property_index := 0; property_index < len(x.Content[0].Content[index+1].Content); property_index++ {
if x.Content[0].Content[index+1].Content[property_index].Value == "car" {
x.Content[0].Content[index+1].Content[property_index+1].SetString("AG1A Jaguar")
}
}
}
}
// show modified yaml
b, err := yaml.Marshal(&x)
if err != nil {
panic(err)
}
fmt.Println(string(b))
// expected:
// # comment
// age: 15
// name: Gita Hartanto alias Tobing
// #this is the hobbies
// hobbies:
// - makan
// - tidur
// # yo, another comment
// - minum
// - pitching qnet
// properties:
// motto: Anda pasti bisa bangkrut
// car: AG1A Jaguar
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment