about summary refs log tree commit diff stats
path: root/internal
diff options
context:
space:
mode:
authorAlan Pearce2024-05-16 20:22:42 +0200
committerAlan Pearce2024-05-16 20:31:03 +0200
commitc51ca7c62e08f43ef9974af35886d30ce54a1f22 (patch)
tree6779f521c289d1750ceb35b57fbb0d3acad20385 /internal
parent0ef52f2f82c62694c5420f7f6c82fb8c45900d1d (diff)
downloadsearchix-c51ca7c62e08f43ef9974af35886d30ce54a1f22.tar.lz
searchix-c51ca7c62e08f43ef9974af35886d30ce54a1f22.tar.zst
searchix-c51ca7c62e08f43ef9974af35886d30ce54a1f22.zip
refactor: use interface to unify Options and Packages
Diffstat (limited to 'internal')
-rw-r--r--internal/importer/importer.go2
-rw-r--r--internal/importer/ingest.go36
-rw-r--r--internal/importer/package.go51
-rw-r--r--internal/importer/utils.go6
-rw-r--r--internal/index/indexer.go22
-rw-r--r--internal/index/search.go4
-rw-r--r--internal/nix/importable.go11
-rw-r--r--internal/nix/option.go37
-rw-r--r--internal/nix/package.go (renamed from internal/packages/package.go)25
-rw-r--r--internal/options/option.go30
-rw-r--r--internal/server/mux.go6
-rw-r--r--internal/server/templates.go4
12 files changed, 130 insertions, 104 deletions
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
diff --git a/internal/index/indexer.go b/internal/index/indexer.go
index 63cf1a6..e1de710 100644
--- a/internal/index/indexer.go
+++ b/internal/index/indexer.go
@@ -10,7 +10,7 @@ import (
 	"os"
 	"path"
 	"searchix/internal/file"
-	"searchix/internal/options"
+	"searchix/internal/nix"
 	"slices"
 
 	"github.com/blevesearch/bleve/v2"
@@ -225,9 +225,9 @@ func OpenOrCreate(dataRoot string, force bool) (*ReadIndex, *WriteIndex, bool, e
 		nil
 }
 
-func (i *WriteIndex) ImportOptions(
+func (i *WriteIndex) Import(
 	ctx context.Context,
-	objects <-chan *options.NixOption,
+	objects <-chan nix.Importable,
 ) <-chan error {
 	var err error
 	errs := make(chan error)
@@ -238,7 +238,7 @@ func (i *WriteIndex) ImportOptions(
 		indexMapping := i.index.Mapping()
 
 	outer:
-		for opt := range objects {
+		for obj := range objects {
 			select {
 			case <-ctx.Done():
 				slog.Debug("context cancelled")
@@ -247,30 +247,30 @@ func (i *WriteIndex) ImportOptions(
 			default:
 			}
 
-			doc := document.NewDocument(opt.Source + "/" + opt.Name)
-			err = indexMapping.MapDocument(doc, opt)
+			doc := document.NewDocument(nix.GetKey(obj))
+			err = indexMapping.MapDocument(doc, obj)
 			if err != nil {
-				errs <- errors.WithMessagef(err, "could not map document for option: %s", opt.Name)
+				errs <- errors.WithMessagef(err, "could not map document for object: %s", obj.GetName())
 
 				continue
 			}
 
 			var data bytes.Buffer
 			enc := gob.NewEncoder(&data)
-			err = enc.Encode(opt)
+			err = enc.Encode(obj)
 			if err != nil {
-				errs <- errors.WithMessage(err, "could not store option in search index")
+				errs <- errors.WithMessage(err, "could not store object in search index")
 
 				continue
 			}
 			field := document.NewTextFieldWithIndexingOptions("_data", nil, data.Bytes(), indexAPI.StoreField)
 			newDoc := doc.AddField(field)
 
-			// slog.Debug("adding option to index", "name", opt.Name)
+			// slog.Debug("adding object to index", "name", opt.Name)
 			err = batch.IndexAdvanced(newDoc)
 
 			if err != nil {
-				errs <- errors.WithMessagef(err, "could not index option %s", opt.Name)
+				errs <- errors.WithMessagef(err, "could not index object %s", obj.GetName())
 
 				continue
 			}
diff --git a/internal/index/search.go b/internal/index/search.go
index d069510..a86cc02 100644
--- a/internal/index/search.go
+++ b/internal/index/search.go
@@ -4,7 +4,7 @@ import (
 	"bytes"
 	"context"
 	"encoding/gob"
-	"searchix/internal/options"
+	"searchix/internal/nix"
 
 	"github.com/blevesearch/bleve/v2"
 	"github.com/blevesearch/bleve/v2/search"
@@ -15,7 +15,7 @@ const ResultsPerPage = 20
 
 type DocumentMatch struct {
 	search.DocumentMatch
-	Data options.NixOption
+	Data nix.Option
 }
 
 type Result struct {
diff --git a/internal/nix/importable.go b/internal/nix/importable.go
new file mode 100644
index 0000000..309ec5f
--- /dev/null
+++ b/internal/nix/importable.go
@@ -0,0 +1,11 @@
+package nix
+
+type Importable interface {
+	BleveType() string
+	GetName() string
+	GetSource() string
+}
+
+func GetKey(i Importable) string {
+	return i.BleveType() + "/" + i.GetSource() + "/" + i.GetName()
+}
diff --git a/internal/nix/option.go b/internal/nix/option.go
new file mode 100644
index 0000000..c1cc4c3
--- /dev/null
+++ b/internal/nix/option.go
@@ -0,0 +1,37 @@
+package nix
+
+type Markdown string
+
+type Value struct {
+	Text     string   `json:",omitempty"`
+	Markdown Markdown `json:",omitempty"`
+}
+
+type Link struct {
+	Name string
+	URL  string
+}
+
+type Option struct {
+	Name            string
+	Source          string
+	Declarations    []Link
+	Default         *Value `json:",omitempty"`
+	Description     Markdown
+	Example         *Value `json:",omitempty"`
+	Loc             []string
+	RelatedPackages Markdown `json:",omitempty"`
+	Type            string
+}
+
+func (Option) BleveType() string {
+	return "option"
+}
+
+func (p Option) GetName() string {
+	return p.Name
+}
+
+func (p Option) GetSource() string {
+	return p.Source
+}
diff --git a/internal/packages/package.go b/internal/nix/package.go
index e864b18..4158d1f 100644
--- a/internal/packages/package.go
+++ b/internal/nix/package.go
@@ -1,21 +1,18 @@
-package packages
+package nix
 
 type Package struct {
-	Name    string
-	Meta    Meta
-	Version string
-}
-
-type Meta struct {
+	Name            string
+	Source          string
 	Broken          bool
+	Definition      string
 	Description     string
-	LongDescription string
 	Homepages       []string
 	Licenses        []License
+	LongDescription string
 	MainProgram     string
 	Maintainers     []Maintainer
 	Platforms       []string
-	Position        string
+	Version         string
 }
 
 type License struct {
@@ -31,6 +28,14 @@ type Maintainer struct {
 	Name   string
 }
 
-func (*Package) BleveType() string {
+func (Package) BleveType() string {
 	return "package"
 }
+
+func (p Package) GetName() string {
+	return p.Name
+}
+
+func (p Package) GetSource() string {
+	return p.Source
+}
diff --git a/internal/options/option.go b/internal/options/option.go
deleted file mode 100644
index a93b4e2..0000000
--- a/internal/options/option.go
+++ /dev/null
@@ -1,30 +0,0 @@
-package options
-
-type Markdown string
-
-type NixValue struct {
-	Text     string   `json:",omitempty"`
-	Markdown Markdown `json:",omitempty"`
-}
-
-type Link struct {
-	Name string
-	URL  string
-}
-
-type NixOption struct {
-	Name   string
-	Source string
-
-	Declarations    []Link
-	Default         *NixValue `json:",omitempty"`
-	Description     Markdown
-	Example         *NixValue `json:",omitempty"`
-	Loc             []string
-	RelatedPackages Markdown `json:",omitempty"`
-	Type            string
-}
-
-func (*NixOption) BleveType() string {
-	return "option"
-}
diff --git a/internal/server/mux.go b/internal/server/mux.go
index af4a51b..78fee60 100644
--- a/internal/server/mux.go
+++ b/internal/server/mux.go
@@ -17,7 +17,7 @@ import (
 	"searchix/frontend"
 	"searchix/internal/config"
 	search "searchix/internal/index"
-	"searchix/internal/options"
+	"searchix/internal/nix"
 
 	"github.com/blevesearch/bleve/v2"
 	sentryhttp "github.com/getsentry/sentry-go/http"
@@ -51,7 +51,7 @@ type TemplateData struct {
 	Message       string
 }
 
-type ResultData[T options.NixOption] struct {
+type ResultData[T nix.Option] struct {
 	TemplateData
 	Query          string
 	ResultsPerPage int
@@ -143,7 +143,7 @@ func NewMux(
 				errorHandler(w, r, err.Error(), http.StatusInternalServerError)
 			}
 
-			tdata := ResultData[options.NixOption]{
+			tdata := ResultData[nix.Option]{
 				TemplateData: TemplateData{
 					ExtraHeadHTML: cfg.Web.ExtraHeadHTML,
 					Source:        *source,
diff --git a/internal/server/templates.go b/internal/server/templates.go
index 1837665..3d45167 100644
--- a/internal/server/templates.go
+++ b/internal/server/templates.go
@@ -8,7 +8,7 @@ import (
 	"log/slog"
 	"path"
 	"searchix/frontend"
-	"searchix/internal/options"
+	"searchix/internal/nix"
 	"strings"
 
 	"github.com/pkg/errors"
@@ -22,7 +22,7 @@ var md = goldmark.New(
 	goldmark.WithExtensions(extension.NewLinkify()),
 )
 var templateFuncs = template.FuncMap{
-	"markdown": func(input options.Markdown) template.HTML {
+	"markdown": func(input nix.Markdown) template.HTML {
 		var out strings.Builder
 		err := md.Convert([]byte(input), &out)
 		if err != nil {