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 | package importer
import (
"context"
"io"
"reflect"
"strings"
"time"
"alin.ovh/x/log"
"alin.ovh/searchix/internal/config"
"alin.ovh/searchix/internal/nix"
"github.com/Southclaws/fault"
"github.com/Southclaws/fault/fmsg"
"github.com/bcicen/jstream"
"github.com/mitchellh/mapstructure"
)
type nixDocJSON struct {
Type string `mapstructure:"_type"`
Text string
}
type linkJSON struct {
Name string
URL string `json:"url"`
}
type nixOptionJSON struct {
Declarations []linkJSON
Default *nixDocJSON
Description string
Example *nixDocJSON
Loc []string
ReadOnly bool
RelatedPackages string
Type string
}
func (i *OptionIngester) convertDocsValue(nj *nixDocJSON) *nix.Docs {
if nj == nil {
return nil
}
switch nj.Type {
case "", "literalExpression":
return &nix.Docs{
Plain: nj.Text,
}
case "literalMD":
return &nix.Docs{
Markdown: nix.Markdown(nj.Text),
}
default:
i.log.Warn("got unexpected docs type", "type", nj.Type, "text", nj.Text)
return nil
}
}
type OptionIngester struct {
dec *jstream.Decoder
ms *mapstructure.Decoder
log *log.Logger
optJSON nixOptionJSON
infile io.ReadCloser
source config.Source
}
func NewOptionProcessor(
infile io.ReadCloser,
source config.Source,
log *log.Logger,
) (*OptionIngester, error) {
i := OptionIngester{
dec: jstream.NewDecoder(infile, source.JSONDepth).EmitKV(),
log: log,
optJSON: nixOptionJSON{},
infile: infile,
source: source,
}
ms, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
ErrorUnused: true,
ZeroFields: true,
Result: &i.optJSON,
Squash: true,
DecodeHook: mapstructure.TextUnmarshallerHookFunc(),
})
if err != nil {
defer infile.Close()
return nil, fault.Wrap(err, fmsg.With("could not create mapstructure decoder"))
}
i.ms = ms
return &i, nil
}
func (i *OptionIngester) Process(
ctx context.Context,
results chan<- nix.Importable,
errs chan<- error,
) {
defer i.infile.Close()
defer close(results)
defer close(errs)
outer:
for mv := range i.dec.Stream() {
select {
case <-ctx.Done():
break outer
default:
}
if err := i.dec.Err(); err != nil {
errs <- fault.Wrap(err, fmsg.With("could not decode JSON"))
continue
}
if mv.ValueType != jstream.Object {
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 := reflect.ValueOf(decl); decl.Kind() {
case reflect.String:
s := decl.String()
link, err := MakeChannelLink(i.source.Repo, s)
if err != nil {
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 reflect.Map:
v := decl.Interface().(map[string]any)
link := nix.Link{
Name: v["name"].(string),
URL: v["url"].(string),
}
decls = append(decls, &link)
default:
errs <- fault.Newf("unexpected declaration type %s", decl.Kind().String())
continue
}
}
if len(decls) > 0 {
x["declarations"] = decls
}
i.optJSON = nixOptionJSON{}
err := i.ms.Decode(x) // stores in optJSON
if err != nil {
errs <- fault.Wrap(err, fmsg.Withf("failed to decode option %#v", x))
continue
}
decs := make([]nix.Link, len(i.optJSON.Declarations))
for i, d := range i.optJSON.Declarations {
decs[i] = nix.Link(d)
}
// log.Debug("sending option", "name", kv.Key)
results <- nix.Option{
Name: kv.Key,
Source: i.source.Key,
Declarations: decs,
Default: i.convertDocsValue(i.optJSON.Default),
Description: nix.Markdown(i.optJSON.Description),
Example: i.convertDocsValue(i.optJSON.Example),
RelatedPackages: nix.Markdown(i.optJSON.RelatedPackages),
Loc: i.optJSON.Loc,
Parents: strings.Join(i.optJSON.Loc[:len(i.optJSON.Loc)-1], "."),
Type: i.optJSON.Type,
ImportedAt: time.Now(),
}
}
}
|