-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add yamldocs as output format, automagically read multi-document YAML…
… files as vectors
- Loading branch information
Showing
5 changed files
with
137 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// SPDX-FileCopyrightText: 2024 Christoph Mewes | ||
// SPDX-License-Identifier: MIT | ||
|
||
package encoding | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
|
||
"go.xrstf.de/rudi/cmd/rudi/types" | ||
|
||
"github.com/BurntSushi/toml" | ||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
func newYamlEncoder(out io.Writer) *yaml.Encoder { | ||
encoder := yaml.NewEncoder(out) | ||
encoder.SetIndent(2) | ||
|
||
return encoder | ||
} | ||
|
||
func Encode(data any, enc types.Encoding, out io.Writer) error { | ||
var encoder interface { | ||
Encode(v any) error | ||
} | ||
|
||
switch enc { | ||
case types.JsonEncoding: | ||
encoder = json.NewEncoder(out) | ||
encoder.(*json.Encoder).SetIndent("", " ") | ||
case types.YamlEncoding: | ||
encoder = newYamlEncoder(out) | ||
case types.YamlDocumentsEncoding: | ||
encoder = &yamldocsEncoder{out: out} | ||
case types.TomlEncoding: | ||
encoder = toml.NewEncoder(out) | ||
encoder.(*toml.Encoder).Indent = " " | ||
default: | ||
encoder = &rawEncoder{out: out} | ||
} | ||
|
||
return encoder.Encode(data) | ||
} | ||
|
||
type rawEncoder struct { | ||
out io.Writer | ||
} | ||
|
||
func (e *rawEncoder) Encode(v any) error { | ||
_, err := fmt.Fprintln(e.out, v) | ||
return err | ||
} | ||
|
||
type yamldocsEncoder struct { | ||
out io.Writer | ||
} | ||
|
||
func (e *yamldocsEncoder) Encode(data any) error { | ||
rValue := reflect.ValueOf(data) | ||
rType := reflect.TypeOf(data) | ||
if rType.Kind() == reflect.Pointer { | ||
rValue = rValue.Elem() | ||
rType = rValue.Type() | ||
} | ||
|
||
encoder := newYamlEncoder(e.out) | ||
|
||
switch rType.Kind() { | ||
case reflect.Slice, reflect.Array: | ||
for i := 0; i < rValue.Len(); i++ { | ||
value := rValue.Index(i).Interface() | ||
if err := encoder.Encode(value); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
return encoder.Encode(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters