Documentation
¶
Overview ¶
Example ¶
Example demonstrates basic usage of the SQLite help system
package main
import (
"context"
"fmt"
"log"
"github.com/go-go-golems/glazed/pkg/help/model"
"github.com/go-go-golems/glazed/pkg/help/store"
)
func main() {
// Create an in-memory store for demonstration
s, err := store.NewInMemory()
if err != nil {
log.Fatal(err)
}
defer func() { _ = s.Close() }()
ctx := context.Background()
// Add some example sections
sections := []*model.Section{
{
Slug: "getting-started",
Title: "Getting Started",
SectionType: model.SectionGeneralTopic,
Content: "This section covers the basics of getting started.",
Topics: []string{"basics", "introduction"},
IsTopLevel: true,
ShowPerDefault: true,
Order: 1,
},
{
Slug: "example-hello",
Title: "Hello World Example",
SectionType: model.SectionExample,
Content: "A simple hello world example.",
Topics: []string{"basics", "examples"},
Commands: []string{"hello"},
ShowPerDefault: true,
Order: 2,
},
{
Slug: "advanced-tutorial",
Title: "Advanced Tutorial",
SectionType: model.SectionTutorial,
Content: "Advanced concepts and techniques.",
Topics: []string{"advanced", "tutorial"},
ShowPerDefault: false,
Order: 3,
},
}
// Add sections to the store
for _, section := range sections {
if err := s.Upsert(ctx, section); err != nil {
log.Fatal(err)
}
}
// Query examples using the predicate system
// 1. Find all top-level sections
topLevel, err := s.Find(ctx, store.IsTopLevel())
if err != nil {
log.Fatal(err)
}
fmt.Printf("Top-level sections: %d\n", len(topLevel))
// 2. Find examples related to "basics"
basicExamples, err := s.Find(ctx, store.And(
store.IsExample(),
store.HasTopic("basics"),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Basic examples: %d\n", len(basicExamples))
// 3. Complex query: Find sections that are either examples OR tutorials AND relate to "basics"
complexQuery, err := s.Find(ctx, store.And(
store.Or(store.IsExample(), store.IsTutorial()),
store.HasTopic("basics"),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Examples or tutorials about basics: %d\n", len(complexQuery))
// 4. Text search (uses LIKE fallback when FTS5 is not available)
searchResults, err := s.Find(ctx, store.TextSearch("hello"))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Search results for 'hello': %d\n", len(searchResults))
// 5. Get sections shown by default, ordered by order field
defaultSections, err := s.Find(ctx, store.And(
store.ShownByDefault(),
store.OrderByOrder(),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Default sections: %d\n", len(defaultSections))
// 6. Count
count, err := s.Count(ctx)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Total sections: %d\n", count)
}
Output: Top-level sections: 1 Basic examples: 1 Examples or tutorials about basics: 1 Search results for 'hello': 1 Default sections: 2 Total sections: 3
Example (AdvancedQuery) ¶
Example_advancedQuery demonstrates more complex querying capabilities
package main
import (
"context"
"fmt"
"log"
"github.com/go-go-golems/glazed/pkg/help/model"
"github.com/go-go-golems/glazed/pkg/help/store"
)
func main() {
s, err := store.NewInMemory()
if err != nil {
log.Fatal(err)
}
defer func() { _ = s.Close() }()
ctx := context.Background()
// Add test data
sections := []*model.Section{
{
Slug: "cmd-help",
Title: "Command Help",
SectionType: model.SectionExample,
Topics: []string{"commands"},
Commands: []string{"help", "man"},
Flags: []string{"--verbose", "--help"},
Order: 1,
},
{
Slug: "config-tutorial",
Title: "Configuration Tutorial",
SectionType: model.SectionTutorial,
Topics: []string{"configuration", "setup"},
Commands: []string{"config"},
Flags: []string{"--config"},
Order: 2,
},
}
for _, section := range sections {
if err := s.Upsert(ctx, section); err != nil {
log.Fatal(err)
}
}
// Complex query: Find sections that have the "help" command OR the "--help" flag
results, err := s.Find(ctx, store.Or(
store.HasCommand("help"),
store.HasFlag("--help"),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Sections with 'help' command or '--help' flag: %d\n", len(results))
// Query with negation: Find all sections that are NOT tutorials
nonTutorials, err := s.Find(ctx, store.Not(store.IsTutorial()))
if err != nil {
log.Fatal(err)
}
fmt.Printf("Non-tutorial sections: %d\n", len(nonTutorials))
// Pagination example: Get first 1 result, then skip 1 and get next 1
firstResult, err := s.Find(ctx, store.And(
store.OrderByOrder(),
store.Limit(1),
))
if err != nil {
log.Fatal(err)
}
secondResult, err := s.Find(ctx, store.And(
store.OrderByOrder(),
store.Limit(1),
store.Offset(1),
))
if err != nil {
log.Fatal(err)
}
fmt.Printf("First result: %s\n", firstResult[0].Slug)
fmt.Printf("Second result: %s\n", secondResult[0].Slug)
}
Output: Sections with 'help' command or '--help' flag: 1 Non-tutorial sections: 1 First result: cmd-help Second result: config-tutorial
Index ¶
- Variables
- type Loader
- func (l *Loader) BatchUpsert(ctx context.Context, sections []*model.Section) error
- func (l *Loader) GetSectionStats(ctx context.Context) (map[string]int64, error)
- func (l *Loader) LoadFromFS(ctx context.Context, filesystem fs.FS, rootDir string) error
- func (l *Loader) LoadFromMarkdown(markdownBytes []byte) (*model.Section, error)
- func (l *Loader) LoadSections(ctx context.Context, markdownFiles map[string][]byte) error
- func (l *Loader) SyncFromFS(ctx context.Context, filesystem fs.FS, rootDir string) error
- type Predicate
- func And(predicates ...Predicate) Predicate
- func ContentContains(term string) Predicate
- func DefaultExamplesForTopic(topic string) Predicate
- func DefaultTutorialsForTopic(topic string) Predicate
- func ExamplesForTopic(topic string) Predicate
- func HasCommand(command string) Predicate
- func HasFlag(flag string) Predicate
- func HasTopic(topic string) Predicate
- func IsApplication() Predicate
- func IsExample() Predicate
- func IsGeneralTopic() Predicate
- func IsTemplate() Predicate
- func IsTopLevel() Predicate
- func IsTutorial() Predicate
- func IsType(sectionType model.SectionType) Predicate
- func Limit(limit int) Predicate
- func Not(predicate Predicate) Predicate
- func NotShownByDefault() Predicate
- func Offset(offset int) Predicate
- func Or(predicates ...Predicate) Predicate
- func OrderByCreatedAt() Predicate
- func OrderByOrder() Predicate
- func OrderByTitle() Predicate
- func ShownByDefault() Predicate
- func SlugEquals(slug string) Predicate
- func SlugIn(slugs []string) Predicate
- func TextSearch(term string) Predicate
- func TitleContains(term string) Predicate
- func TopLevelDefaults() Predicate
- func TutorialsForTopic(topic string) Predicate
- type QueryCompiler
- func (qc *QueryCompiler) AddJoin(join string)
- func (qc *QueryCompiler) AddWhere(condition string, args ...interface{})
- func (qc *QueryCompiler) BuildQuery() (string, []interface{})
- func (qc *QueryCompiler) SetLimit(limit int)
- func (qc *QueryCompiler) SetOffset(offset int)
- func (qc *QueryCompiler) SetOrderBy(orderBy string)
- type Store
- func (s *Store) Clear(ctx context.Context) error
- func (s *Store) Close() error
- func (s *Store) Count(ctx context.Context) (int64, error)
- func (s *Store) Delete(ctx context.Context, id int64) error
- func (s *Store) Find(ctx context.Context, predicate Predicate) ([]*model.Section, error)
- func (s *Store) GetByID(ctx context.Context, id int64) (*model.Section, error)
- func (s *Store) GetBySlug(ctx context.Context, slug string) (*model.Section, error)
- func (s *Store) Insert(ctx context.Context, section *model.Section) error
- func (s *Store) List(ctx context.Context, orderBy string) ([]*model.Section, error)
- func (s *Store) Update(ctx context.Context, section *model.Section) error
- func (s *Store) Upsert(ctx context.Context, section *model.Section) error
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrSectionNotFound = errors.New("section not found")
ErrSectionNotFound is returned by GetBySlug and GetByID when no matching section exists in the store. Callers should use errors.Is to test for it.
Functions ¶
This section is empty.
Types ¶
type Loader ¶
type Loader struct {
// contains filtered or unexported fields
}
Loader handles loading and syncing markdown files into the SQLite store
func (*Loader) BatchUpsert ¶
BatchUpsert performs bulk upsert of sections
func (*Loader) GetSectionStats ¶
GetSectionStats returns statistics about the loaded sections
func (*Loader) LoadFromFS ¶
LoadFromFS loads all markdown files from a filesystem into the store
func (*Loader) LoadFromMarkdown ¶
LoadFromMarkdown parses a markdown file and returns a Section. Delegates to the canonical model.ParseSectionFromMarkdown.
func (*Loader) LoadSections ¶
LoadSections loads multiple sections from markdown bytes
type Predicate ¶
type Predicate func(*QueryCompiler)
Predicate represents a query predicate that can be compiled to SQL
func ContentContains ¶
ContentContains filters sections where content contains the term
func DefaultExamplesForTopic ¶
DefaultExamplesForTopic filters for default examples related to a specific topic
func DefaultTutorialsForTopic ¶
DefaultTutorialsForTopic filters for default tutorials related to a specific topic
func ExamplesForTopic ¶
ExamplesForTopic filters for examples related to a specific topic
func HasCommand ¶
HasCommand filters sections that have a specific command
func IsGeneralTopic ¶
func IsGeneralTopic() Predicate
IsGeneralTopic filters for general topic sections
func NotShownByDefault ¶
func NotShownByDefault() Predicate
NotShownByDefault filters sections that are not shown by default
func OrderByCreatedAt ¶
func OrderByCreatedAt() Predicate
OrderByCreatedAt sorts sections by creation time
func ShownByDefault ¶
func ShownByDefault() Predicate
ShownByDefault filters sections that are shown by default
func SlugEquals ¶
SlugEquals filters sections by exact slug match
func TextSearch ¶
TextSearch performs a fallback text search using LIKE when FTS5 is not available
func TitleContains ¶
TitleContains filters sections where title contains the term
func TopLevelDefaults ¶
func TopLevelDefaults() Predicate
TopLevelDefaults filters for top-level sections shown by default
func TutorialsForTopic ¶
TutorialsForTopic filters for tutorials related to a specific topic
type QueryCompiler ¶
type QueryCompiler struct {
// contains filtered or unexported fields
}
QueryCompiler builds SQL queries from predicates
func NewQueryCompiler ¶
func NewQueryCompiler() *QueryCompiler
NewQueryCompiler creates a new query compiler
func (*QueryCompiler) AddJoin ¶
func (qc *QueryCompiler) AddJoin(join string)
AddJoin adds a JOIN clause
func (*QueryCompiler) AddWhere ¶
func (qc *QueryCompiler) AddWhere(condition string, args ...interface{})
AddWhere adds a WHERE clause condition
func (*QueryCompiler) BuildQuery ¶
func (qc *QueryCompiler) BuildQuery() (string, []interface{})
BuildQuery builds the complete SQL query
func (*QueryCompiler) SetLimit ¶
func (qc *QueryCompiler) SetLimit(limit int)
SetLimit sets the LIMIT clause
func (*QueryCompiler) SetOffset ¶
func (qc *QueryCompiler) SetOffset(offset int)
SetOffset sets the OFFSET clause
func (*QueryCompiler) SetOrderBy ¶
func (qc *QueryCompiler) SetOrderBy(orderBy string)
SetOrderBy sets the ORDER BY clause
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store represents the SQLite-backed help system storage
func NewInMemory ¶
NewInMemory creates a new in-memory SQLite store