test: encode test program
This commit is contained in:
70
tools/encode/encyaml.go
Normal file
70
tools/encode/encyaml.go
Normal file
@@ -0,0 +1,70 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func printYAML(node *yaml.Node) {
|
||||
switch node.Kind {
|
||||
case yaml.MappingNode:
|
||||
fmt.Print(node.HeadComment)
|
||||
fmt.Print("{")
|
||||
for i := 0; i < len(node.Content)/2; i++ {
|
||||
key := node.Content[i*2]
|
||||
value := node.Content[i*2+1]
|
||||
printYAML(key)
|
||||
fmt.Print(": ")
|
||||
printYAML(value)
|
||||
if i < len(node.Content)/2-1 {
|
||||
fmt.Print(", ")
|
||||
}
|
||||
}
|
||||
fmt.Print("}")
|
||||
fmt.Print(node.FootComment)
|
||||
case yaml.SequenceNode:
|
||||
fmt.Print("[")
|
||||
for i, n := range node.Content {
|
||||
printYAML(n)
|
||||
if i < len(node.Content)-1 {
|
||||
fmt.Print(", ")
|
||||
}
|
||||
}
|
||||
fmt.Print("]")
|
||||
default:
|
||||
fmt.Print(node.Value)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
data := `
|
||||
test:
|
||||
smfSystem:
|
||||
display: "SMF System"
|
||||
list:
|
||||
- name: "sbiIpAddr"
|
||||
type: "string"
|
||||
value: "172.16.5.150"
|
||||
access: "read-write"
|
||||
filter: ''
|
||||
display: "SBI IP"
|
||||
comment: ""
|
||||
amf:
|
||||
smfSystem:
|
||||
display: "SMF System"
|
||||
list:
|
||||
- name: "sbiIpAddr"
|
||||
type: "string"
|
||||
value: "172.16.5.150"
|
||||
access: "read-write"
|
||||
filter: ''
|
||||
display: "SBI IP"
|
||||
comment: ""
|
||||
`
|
||||
var node yaml.Node
|
||||
if err := yaml.Unmarshal([]byte(data), &node); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
printYAML(&node)
|
||||
}
|
||||
Reference in New Issue
Block a user