Home

ffdhall @main - refs - log -
-
https://git.jolheiser.com/ffdhall.git
dhall parser for peterbourgon/ff
ffdhall / ffdhall.go
- raw
 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
70
71
72
package ffdhall

import (
	"errors"
	"fmt"
	"io"
	"strings"

	"github.com/philandstuff/dhall-golang/v6"
)

// DhallParser is a helper function that uses a default DhallParseConfig.
func DhallParser(r io.Reader, set func(name, value string) error) error {
	return (&DhallParseConfig{}).Parse(r, set)
}

// DhallParseConfig collects parameters for the Dhall config file parser.
type DhallParseConfig struct {
	// Delimiter is used when concatenating nested node keys into a flag name.
	// The default delimiter is ".".
	Delimiter string
}

// Parse a Dhall document from the provided io.Reader, using the provided set
// function to set flag values. Flag names are derived from the node names and
// their key/value pairs.
func (pc *DhallParseConfig) Parse(r io.Reader, set func(name, value string) error) error {
	if pc.Delimiter == "" {
		pc.Delimiter = "."
	}

	data, err := io.ReadAll(r)
	if err != nil {
		return err
	}

	// Compatibility with other parsers
	if strings.TrimSpace(string(data)) == "" {
		return nil
	}

	var m interface{}
	if err := dhall.Unmarshal(data, &m); err != nil {
		return DhallParseError{Inner: err}
	}
	mm, ok := m.(map[string]interface{})
	if !ok {
		return errors.New("could not unmarshal to map[string]interface{}")
	}

	if err := traverseMap(mm, pc.Delimiter, set); err != nil {
		return DhallParseError{Inner: err}
	}

	return nil
}

// DhallParseError wraps all errors originating from the DhallParser.
type DhallParseError struct {
	Inner error
}

// Error implenents the error interface.
func (e DhallParseError) Error() string {
	return fmt.Sprintf("could not parse Dhall config: %v", e.Inner)
}

// Unwrap implements the errors.Wrapper interface, allowing errors.Is and
// errors.As to work with DhallParseErrors.
func (e DhallParseError) Unwrap() error {
	return e.Inner
}