From c51ca7c62e08f43ef9974af35886d30ce54a1f22 Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Thu, 16 May 2024 20:22:42 +0200 Subject: refactor: use interface to unify Options and Packages --- internal/importer/importer.go | 2 +- internal/importer/ingest.go | 36 +++++++++++++++--------------- internal/importer/package.go | 51 ++++++++++++++++++++++--------------------- internal/importer/utils.go | 6 ++--- 4 files changed, 49 insertions(+), 46 deletions(-) (limited to 'internal/importer') diff --git a/internal/importer/importer.go b/internal/importer/importer.go index 0768656..255f70e 100644 --- a/internal/importer/importer.go +++ b/internal/importer/importer.go @@ -75,7 +75,7 @@ func processOptions( options, pErrs := processor.Process(ctx) wg.Add(1) - iErrs := indexer.ImportOptions(ctx, options) + iErrs := indexer.Import(ctx, options) var hadErrors bool go func() { diff --git a/internal/importer/ingest.go b/internal/importer/ingest.go index 778a4ae..9b92ae8 100644 --- a/internal/importer/ingest.go +++ b/internal/importer/ingest.go @@ -6,7 +6,7 @@ import ( "os" "reflect" "searchix/internal/config" - "searchix/internal/options" + "searchix/internal/nix" "github.com/bcicen/jstream" "github.com/mitchellh/mapstructure" @@ -34,21 +34,21 @@ type nixOptionJSON struct { Type string } -func convertNixValue(nj *nixValueJSON) *options.NixValue { +func convertValue(nj *nixValueJSON) *nix.Value { if nj == nil { return nil } switch nj.Type { case "", "literalExpression": - return &options.NixValue{ + return &nix.Value{ Text: nj.Text, } case "literalMD": - return &options.NixValue{ - Markdown: options.Markdown(nj.Text), + return &nix.Value{ + Markdown: nix.Markdown(nj.Text), } default: - slog.Warn("got unexpected NixValue type", "type", nj.Type, "text", nj.Text) + slog.Warn("got unexpected Value type", "type", nj.Type, "text", nj.Text) return nil } @@ -91,8 +91,10 @@ func NewOptionProcessor(inpath string, source *config.Source) (*OptionIngester, return &i, nil } -func (i *OptionIngester) Process(ctx context.Context) (<-chan *options.NixOption, <-chan error) { - results := make(chan *options.NixOption) +func (i *OptionIngester) Process( + ctx context.Context, +) (<-chan nix.Importable, <-chan error) { + results := make(chan nix.Importable) errs := make(chan error) go func() { @@ -121,7 +123,7 @@ func (i *OptionIngester) Process(ctx context.Context) (<-chan *options.NixOption kv := mv.Value.(jstream.KV) x := kv.Value.(map[string]interface{}) - var decls []*options.Link + var decls []*nix.Link for _, decl := range x["declarations"].([]interface{}) { i.optJSON = nixOptionJSON{} @@ -140,7 +142,7 @@ func (i *OptionIngester) Process(ctx context.Context) (<-chan *options.NixOption decls = append(decls, link) case reflect.Map: v := decl.Interface().(map[string]interface{}) - link := options.Link{ + link := nix.Link{ Name: v["name"].(string), URL: v["url"].(string), } @@ -162,20 +164,20 @@ func (i *OptionIngester) Process(ctx context.Context) (<-chan *options.NixOption continue } - var decs = make([]options.Link, len(i.optJSON.Declarations)) + var decs = make([]nix.Link, len(i.optJSON.Declarations)) for i, d := range i.optJSON.Declarations { - decs[i] = options.Link(d) + decs[i] = nix.Link(d) } // slog.Debug("sending option", "name", kv.Key) - results <- &options.NixOption{ + results <- nix.Option{ Name: kv.Key, Source: i.source.Key, Declarations: decs, - Default: convertNixValue(i.optJSON.Default), - Description: options.Markdown(i.optJSON.Description), - Example: convertNixValue(i.optJSON.Example), - RelatedPackages: options.Markdown(i.optJSON.RelatedPackages), + Default: convertValue(i.optJSON.Default), + Description: nix.Markdown(i.optJSON.Description), + Example: convertValue(i.optJSON.Example), + RelatedPackages: nix.Markdown(i.optJSON.RelatedPackages), Loc: i.optJSON.Loc, Type: i.optJSON.Type, } diff --git a/internal/importer/package.go b/internal/importer/package.go index c0f5f93..a1bc9fb 100644 --- a/internal/importer/package.go +++ b/internal/importer/package.go @@ -7,7 +7,7 @@ import ( "os" "reflect" "searchix/internal/config" - "searchix/internal/packages" + "searchix/internal/nix" "strings" "github.com/bcicen/jstream" @@ -45,8 +45,8 @@ type PackageIngester struct { source *config.Source } -func makeAdhocLicense(name string) packages.License { - return packages.License{ +func makeAdhocLicense(name string) nix.License { + return nix.License{ FullName: name, } } @@ -88,8 +88,8 @@ func NewPackageProcessor(inpath string, source *config.Source) (*PackageIngester return i, nil } -func convertToLicense(in map[string]any) *packages.License { - l := &packages.License{} +func convertToLicense(in map[string]any) *nix.License { + l := &nix.License{} if v, found := in["shortName"]; found { l.Name = v.(string) } @@ -109,8 +109,10 @@ func convertToLicense(in map[string]any) *packages.License { return l } -func (i *PackageIngester) Process(ctx context.Context) (<-chan *packages.Package, <-chan error) { - results := make(chan *packages.Package) +func (i *PackageIngester) Process( + ctx context.Context, +) (<-chan nix.Importable, <-chan error) { + results := make(chan nix.Importable) errs := make(chan error) go func() { @@ -143,13 +145,13 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan *packages.Package meta := x["meta"].(map[string]interface{}) - var licenses []packages.License + var licenses []nix.License if meta["license"] != nil { switch v := reflect.ValueOf(meta["license"]); v.Kind() { case reflect.Map: licenses = append(licenses, *convertToLicense(v.Interface().(map[string]interface{}))) case reflect.Array, reflect.Slice: - licenses = make([]packages.License, v.Len()) + licenses = make([]nix.License, v.Len()) for i, v := range v.Interface().([]interface{}) { switch v := reflect.ValueOf(v); v.Kind() { case reflect.String: @@ -214,9 +216,9 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan *packages.Package continue } - maintainers := make([]packages.Maintainer, len(i.pkg.Meta.Maintainers)) + maintainers := make([]nix.Maintainer, len(i.pkg.Meta.Maintainers)) for i, m := range i.pkg.Meta.Maintainers { - maintainers[i] = packages.Maintainer{ + maintainers[i] = nix.Maintainer{ Name: m.Name, Github: m.Github, } @@ -224,20 +226,19 @@ func (i *PackageIngester) Process(ctx context.Context) (<-chan *packages.Package subpath, line, _ := strings.Cut(i.pkg.Meta.Position, ":") - results <- &packages.Package{ - Name: i.pkg.Name, - Version: i.pkg.Version, - Meta: packages.Meta{ - Broken: i.pkg.Meta.Broken, - Description: i.pkg.Meta.Description, - LongDescription: i.pkg.Meta.LongDescription, - Homepages: i.pkg.Meta.Homepages, - Licenses: licenses, - MainProgram: i.pkg.Meta.MainProgram, - Platforms: i.pkg.Meta.Platforms, - Maintainers: maintainers, - Position: makeGitHubFileURL(userRepo, "", subpath, line), - }, + results <- nix.Package{ + Name: i.pkg.Name, + Source: i.source.Key, + Version: i.pkg.Version, + Broken: i.pkg.Meta.Broken, + Description: i.pkg.Meta.Description, + LongDescription: i.pkg.Meta.LongDescription, + Homepages: i.pkg.Meta.Homepages, + MainProgram: i.pkg.Meta.MainProgram, + Platforms: i.pkg.Meta.Platforms, + Licenses: licenses, + Maintainers: maintainers, + Definition: makeGitHubFileURL(userRepo, "", subpath, line), } } }() diff --git a/internal/importer/utils.go b/internal/importer/utils.go index f7ba36a..13d4702 100644 --- a/internal/importer/utils.go +++ b/internal/importer/utils.go @@ -3,7 +3,7 @@ package importer import ( "fmt" "net/url" - "searchix/internal/options" + "searchix/internal/nix" "github.com/bcicen/jstream" ) @@ -48,12 +48,12 @@ var channelRepoMap = map[string]string{ "home-manager": "nix-community/home-manager", } -func MakeChannelLink(channel string, ref string, subPath string) (*options.Link, error) { +func MakeChannelLink(channel string, ref string, subPath string) (*nix.Link, error) { if channelRepoMap[channel] == "" { return nil, fmt.Errorf("don't know what repository relates to channel <%s>", channel) } - return &options.Link{ + return &nix.Link{ Name: fmt.Sprintf("<%s/%s>", channel, subPath), URL: makeGitHubFileURL(channelRepoMap[channel], ref, subPath, ""), }, nil -- cgit 1.4.1