all repos — searchix @ link-other-instance

Search engine for NixOS, nix-darwin, home-manager and NUR users

internal/importer/options.go (view 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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package importer

import (
	"context"
	"io"
	"iter"
	"reflect"
	"strings"
	"time"

	"alin.ovh/x/log"

	"alin.ovh/searchix/internal/config"
	"alin.ovh/searchix/internal/index"
	"alin.ovh/searchix/internal/nix"
	"alin.ovh/searchix/internal/nixdocs"

	"github.com/Southclaws/fault"
	"github.com/Southclaws/fault/fmsg"
	"github.com/bcicen/jstream"
)

func (i *OptionIngester) convertDocsValue(docMap map[string]any) *nix.Docs {
	if docMap == nil {
		return nil
	}

	var docType string
	if t, ok := docMap["_type"].(string); ok {
		docType = t
	}

	var text string
	if t, ok := docMap["text"].(string); ok {
		text = t
	}

	switch docType {
	case "", "literalExpression":
		return &nix.Docs{
			Plain: text,
		}
	case "literalMD":
		return &nix.Docs{
			Markdown: nixdocs.Markdown(text),
		}
	default:
		i.log.Warn("got unexpected docs type", "type", docType, "text", text)

		return nil
	}
}

type OptionIngester struct {
	dec    *jstream.Decoder
	log    *log.Logger
	infile io.ReadCloser
	source config.Source
	errs   []error
}

func NewOptionProcessor(
	infile io.ReadCloser,
	source config.Source,
	log *log.Logger,
) *OptionIngester {
	return &OptionIngester{
		dec:    jstream.NewDecoder(infile, source.JSONDepth).EmitKV(),
		log:    log,
		infile: infile,
		source: source,
	}
}

func (i *OptionIngester) Process(
	ctx context.Context,
) iter.Seq[index.Indexable] {
	return func(yield func(index.Indexable) bool) {
		defer i.infile.Close()

		for mv := range i.dec.Stream() {
			if ctx.Err() != nil {
				break
			}
			if err := i.dec.Err(); err != nil {
				i.errs = append(i.errs, fault.Wrap(err, fmsg.With("could not decode JSON")))

				continue
			}
			if mv.ValueType != jstream.Object {
				i.errs = append(
					i.errs,
					fault.Newf("unexpected object type %s", ValueTypeToString(mv.ValueType)),
				)

				continue
			}
			kv := mv.Value.(jstream.KV)
			x := kv.Value.(map[string]any)

			var decls []nix.Link
			for _, decl := range x["declarations"].([]any) {
				switch decl := decl.(type) {
				case string:
					s := decl
					link, err := MakeChannelLink(i.source.Repo, s)
					if err != nil {
						i.errs = append(
							i.errs,
							fault.Wrap(
								err,
								fmsg.Withf(
									"could not make a channel link for channel %s, revision %s and subpath %s",
									i.source.Channel,
									i.source.Repo.Revision,
									s,
								),
							),
						)

						continue
					}
					decls = append(decls, *link)
				case map[string]any:
					v := decl
					link := nix.Link{
						Name: v["name"].(string),
						URL:  v["url"].(string),
					}
					decls = append(decls, link)
				default:
					i.errs = append(
						i.errs,
						fault.Newf("unexpected declaration type %s", reflect.TypeOf(decl).String()),
					)

					continue
				}
			}
			var description string
			if v, ok := x["description"].(string); ok {
				description = v
			}

			var optType string
			if v, ok := x["type"].(string); ok {
				optType = v
			}

			var relatedPackages string
			if v, ok := x["relatedPackages"].(string); ok {
				relatedPackages = v
			}

			var loc []string
			if v, ok := x["loc"].([]any); ok {
				loc = make([]string, len(v))
				for i, item := range v {
					if s, ok := item.(string); ok {
						loc[i] = s
					}
				}
			}

			var defaultDocs *nix.Docs
			if v, ok := x["default"].(map[string]any); ok {
				defaultDocs = i.convertDocsValue(v)
			}

			var exampleDocs *nix.Docs
			if v, ok := x["example"].(map[string]any); ok {
				exampleDocs = i.convertDocsValue(v)
			}

			var optionSet string
			var parents []string
			if len(loc) >= 1 {
				// Top-level parent -> optionSet
				optionSet = loc[0]

				for i := 1; i < len(loc); i++ {
					parents = append(parents,
						strings.Join(loc[:i], "."),
					)
				}
			}

			// log.Debug("sending option", "name", kv.Key)
			if !yield(nix.Option{
				Name:            kv.Key,
				Source:          i.source.Key,
				Declarations:    decls,
				Default:         defaultDocs,
				Description:     nixdocs.Markdown(description),
				Example:         exampleDocs,
				RelatedPackages: nixdocs.Markdown(relatedPackages),
				Loc:             loc,
				Parents:         parents,
				OptionSet:       optionSet,
				Type:            optType,
				ImportedAt:      time.Now(),
			}) {
				return
			}
		}
	}
}

func (i *OptionIngester) Err() error {
	errs := i.errs
	if len(errs) == 0 {
		return nil
	}
	if len(errs) == 1 {
		return i.errs[0]
	}

	i.errs = []error{}

	return fault.Newf("encountered %d errors during processing", len(errs))
}