Skip to content

Commit

Permalink
Added examlple golang parcer yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
k8s4 committed Oct 29, 2024
1 parent b76cacf commit 724dfba
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 4 deletions.
15 changes: 15 additions & 0 deletions examples/golang/cource/06f_formats/examples/anchor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
definitions:
steps:
- step: &build-test
name: Build and go fu
script:
- mvn package
artifacts:
- target/**

pipelines:
branches:
develop:
- step: *build-test
main:
- step: *build-test
3 changes: 3 additions & 0 deletions examples/golang/cource/06f_formats/examples/custom.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
messages:
tags: Something, In, The, Water

4 changes: 4 additions & 0 deletions examples/golang/cource/06f_formats/examples/example.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
some: 44
time: 1834234325
...
5 changes: 5 additions & 0 deletions examples/golang/cource/06f_formats/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module 06f_formats

go 1.23.2

require gopkg.in/yaml.v3 v3.0.1 // indirect
3 changes: 3 additions & 0 deletions examples/golang/cource/06f_formats/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 4 additions & 4 deletions examples/golang/cource/06f_formats/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,22 @@ func CreateJson() string {
}

func LoadAndParceJson() {
jsonData, err := os.ReadFile("example.json")
jsonData, err := os.ReadFile("examples/example.json")
fmt.Println(err)
var request Request
fmt.Println(json.Unmarshal(jsonData, &request))
fmt.Println(request)
}

func LoadAndParseRawMsgToMap() {
jsonData, _ := os.ReadFile("example.json")
jsonData, _ := os.ReadFile("examples/example.json")
var objmap map[string]interface{}
json.Unmarshal(jsonData, &objmap)
fmt.Println(objmap)
}

func LoadAndParseRawMsg() {
jsonData, _ := os.ReadFile("example.json")
jsonData, _ := os.ReadFile("examples/example.json")
var objmap map[string]json.RawMessage
json.Unmarshal(jsonData, &objmap)
fmt.Println(objmap)
Expand All @@ -109,7 +109,7 @@ func LoadAndParseRawMsg() {
}

func LoadAndParseJsonToCustomStruct() {
jsonData, err := os.ReadFile("custom.json")
jsonData, err := os.ReadFile("examples/custom.json")
fmt.Println(err)
var request RequestTagged
fmt.Println(json.Unmarshal(jsonData, &request))
Expand Down
86 changes: 86 additions & 0 deletions examples/golang/cource/06f_formats/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

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

type BuildConf struct {
Definitions map[string]interface{} `yaml:"definitions"`
Pipelines map[string]interface{} `yaml:"pipelines"`
}

type Shon struct {
Some int64 `yaml:"some"`
Time int64 `yaml:"time"`
}

type SlicesTags []string

func (tags *SlicesTags) UnmarshalYAML(value *yaml.Node) error {
if value != nil {
*tags = strings.Split(value.Value, ",")
}
return nil
}

type Messages struct {
Tags SlicesTags `yaml:"tags"`
}

type Subs struct {
Messages Messages `yaml:"messages"`
}

func ParseYamlWithCustomStruct() {
config := &Subs{}

file, err := ioutil.ReadFile("examples/custom.yaml")
if err != nil {
log.Printf("Read file err: %v", err)
}
err = yaml.Unmarshal(file, config)
if err != nil {
log.Fatalf("Unmarshal err: %v", err)
}
fmt.Println(*config)
}

func GetShon() {
shon := &Shon{}

file, err := ioutil.ReadFile("examples/example.yaml")
if err != nil {
log.Printf("Read file err: %v", err)
}
err = yaml.Unmarshal(file, shon)
if err != nil {
log.Fatalf("Unmarshal err: %v", err)
}

fmt.Println(*shon)
}

func ReadAnchoredYaml() {
conf := &BuildConf{}

file, err := ioutil.ReadFile("examples/anchor.yaml")
if err != nil {
log.Printf("Read file err: %v", err)
}
err = yaml.Unmarshal(file, conf)
if err != nil {
log.Fatalf("Unmarshal err: %v", err)
}

fmt.Println("Second, Ancor: ", conf)
}

func main() {
GetShon()
ReadAnchoredYaml()
ParseYamlWithCustomStruct()
}

0 comments on commit 724dfba

Please sign in to comment.