about summary refs log tree commit diff stats
path: root/internal/options/process.go
blob: 9721a7b33a228d16892aa0dbafa6280729832356 (plain)
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package options

import (
	"encoding/json"
	"io"
	"log/slog"
	"os"

	"github.com/bcicen/jstream"
	"github.com/mitchellh/mapstructure"
	"github.com/pkg/errors"
)

type nixValueJSON struct {
	Type string `mapstructure:"_type"`
	Text string
}

type linkJSON struct {
	Name string
	URL  string `json:"url"`
}

type nixOptionJSON struct {
	Declarations    []linkJSON
	Default         nixValueJSON
	Description     string
	Example         nixValueJSON
	Loc             []string
	ReadOnly        bool
	RelatedPackages string
	Type            string
}

func ValueTypeToString(valueType jstream.ValueType) string {
	switch valueType {
	case jstream.Unknown:
		return "unknown"
	case jstream.Null:
		return "null"
	case jstream.String:
		return "string"
	case jstream.Number:
		return "number"
	case jstream.Boolean:
		return "boolean"
	case jstream.Array:
		return "array"
	case jstream.Object:
		return "object"
	}

	return "very strange"
}

func convertNixValue(nj nixValueJSON) *NixValue {
	switch nj.Type {
	case "", "literalExpression":
		if nj.Text == "" {
			return nil
		}

		return &NixValue{
			Text: nj.Text,
		}
	case "literalMD":
		return &NixValue{
			Markdown: Markdown(nj.Text),
		}
	default:
		slog.Warn("got unexpected NixValue type", "type", nj.Type, "text", nj.Text)

		return nil
	}
}

func Process(inpath string, outpath string) error {
	infile, err := os.Open(inpath)
	if err != nil {
		return errors.WithMessagef(err, "failed to open input file %s", inpath)
	}
	defer infile.Close()
	outfile, err := os.Create(outpath)
	if err != nil {
		return errors.WithMessagef(err, "failed to open output file %s", outpath)
	}
	if outpath != "/dev/stdout" {
		defer outfile.Close()
	}

	dec := jstream.NewDecoder(infile, 1).EmitKV()
	var optJSON nixOptionJSON
	ms, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		ErrorUnused: true,
		ZeroFields:  true,
		Result:      &optJSON,
		Squash:      true,
		DecodeHook:  mapstructure.TextUnmarshallerHookFunc(),
	})
	if err != nil {
		return errors.WithMessage(err, "could not create mapstructure decoder")
	}

	_, err = outfile.WriteString("[\n")
	if err != nil {
		return errors.WithMessage(err, "could not write to output")
	}
	for mv := range dec.Stream() {
		if err := dec.Err(); err != nil {
			return errors.WithMessage(err, "could not decode JSON")
		}
		if mv.ValueType != jstream.Object {
			return errors.Errorf("unexpected object type %s", ValueTypeToString(mv.ValueType))
		}
		kv := mv.Value.(jstream.KV)
		x := kv.Value.(map[string]interface{})

		err = ms.Decode(x) // stores in optJSON
		if err != nil {
			return errors.WithMessagef(err, "failed to decode option %#v", x)
		}

		var decs = make([]Link, len(optJSON.Declarations))
		for i, d := range optJSON.Declarations {
			decs[i] = Link(d)
		}

		opt := NixOption{
			Option:          kv.Key,
			Declarations:    decs,
			Default:         convertNixValue(optJSON.Default),
			Description:     Markdown(optJSON.Description),
			Example:         convertNixValue(optJSON.Example),
			RelatedPackages: Markdown(optJSON.RelatedPackages),
			Loc:             optJSON.Loc,
			Type:            optJSON.Type,
		}

		b, err := json.MarshalIndent(opt, "", "  ")
		if err != nil {
			return errors.WithMessagef(err, "failed to encode option %#v", opt)
		}

		_, err = outfile.Write(b)
		if err != nil {
			return errors.WithMessage(err, "failed to write to output")
		}
		_, err = outfile.WriteString(",\n")
		if err != nil {
			return errors.WithMessage(err, "failed to write to output")
		}
	}

	if outpath != "/dev/stdout" {
		_, err = outfile.Seek(-2, io.SeekCurrent)
		if err != nil {
			return errors.WithMessage(err, "could not write to output")
		}
	}

	_, err = outfile.WriteString("]\n")
	if err != nil {
		return errors.WithMessage(err, "could not write to output")
	}

	return nil
}