generated from adrienaury/go-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextReaderYAML.go
69 lines (55 loc) · 1.66 KB
/
contextReaderYAML.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (C) 2023 CGI France
//
// This file is part of emporte-piece.
//
// Emporte-piece is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Emporte-piece is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with emporte-piece. If not, see <http://www.gnu.org/licenses/>.
package infra
import (
"fmt"
"io"
"os"
"gopkg.in/yaml.v3"
)
type ContextReaderYAML struct {
read bool
input io.Reader
}
func NewContextReaderYAML(input io.Reader) *ContextReaderYAML {
return &ContextReaderYAML{read: false, input: input}
}
func NewContextReaderYAMLFromFile(filepath string) (*ContextReaderYAML, error) {
input, err := os.Open(filepath)
if err != nil {
return nil, fmt.Errorf("error opening file: %w", err)
}
return NewContextReaderYAML(input), nil
}
func (cr *ContextReaderYAML) HasNext() bool {
return !cr.read
}
func (cr *ContextReaderYAML) Next() (any, error) {
if cr.read {
return nil, ErrContextReaderEmpty
}
cr.read = true
bytes, err := io.ReadAll(cr.input)
if err != nil {
return nil, fmt.Errorf("error reading JSON input: %w", err)
}
context := make(map[string]any)
if err := yaml.Unmarshal(bytes, &context); err != nil {
return nil, fmt.Errorf("error parsing JSON input: %w", err)
}
return context, nil
}