fsedsl: simplify README usage section
This commit is contained in:
parent
aaacae40a8
commit
edc38f7353
107
fsedsl/README.md
107
fsedsl/README.md
|
|
@ -105,61 +105,37 @@ func main() {
|
|||
}
|
||||
```
|
||||
|
||||
## Integration Guide
|
||||
## Usage
|
||||
|
||||
For larger projects, create an adapter package that specializes fsedsl for your context type.
|
||||
|
||||
### Step 1: Define your context type and type aliases
|
||||
Define your context type, then create type and function aliases that bake it in:
|
||||
|
||||
```go
|
||||
// myproject/fs/adapter.go
|
||||
package fs
|
||||
|
||||
import "git.lneely.de/lkn/fsedsl"
|
||||
|
||||
// Ctx carries runtime dependencies for handler functions.
|
||||
type Ctx struct {
|
||||
DB *Database
|
||||
User *User
|
||||
Session *Session
|
||||
DB *Database
|
||||
User *User
|
||||
}
|
||||
|
||||
// Type aliases for external use
|
||||
type FsNodeDecl = fsedsl.FsNodeDecl[Ctx]
|
||||
type NodeOption = fsedsl.NodeOption[Ctx]
|
||||
type Binding = fsedsl.Binding[Ctx]
|
||||
type Tree = fsedsl.Tree
|
||||
type File = fsedsl.File
|
||||
```
|
||||
// Type aliases for external consumers
|
||||
type (
|
||||
FsNodeDecl = fsedsl.FsNodeDecl[Ctx]
|
||||
Binding = fsedsl.Binding[Ctx]
|
||||
Tree = fsedsl.Tree
|
||||
)
|
||||
|
||||
### Step 2: Create local function aliases
|
||||
|
||||
In the file where you define your spec, create package-local variable aliases
|
||||
that bake in your context type. This keeps the spec clean without requiring
|
||||
`[Ctx]` on every call:
|
||||
|
||||
```go
|
||||
// myproject/fs/spec.go
|
||||
package fs
|
||||
|
||||
import "git.lneely.de/lkn/fsedsl"
|
||||
|
||||
// Local aliases for fsedsl constructors, specialized to Ctx.
|
||||
// Function aliases for clean spec definitions
|
||||
var (
|
||||
DirNode = fsedsl.DirNode[Ctx]
|
||||
FileNode = fsedsl.FileNode[Ctx]
|
||||
Each = fsedsl.Each[Ctx]
|
||||
Doc = fsedsl.Doc[Ctx]
|
||||
Read = fsedsl.Read[Ctx]
|
||||
Write = fsedsl.Write[Ctx]
|
||||
Stream = fsedsl.Stream[Ctx]
|
||||
BlockOnce = fsedsl.BlockOnce[Ctx]
|
||||
Request = fsedsl.Request[Ctx]
|
||||
Remove = fsedsl.RemoveNode[Ctx]
|
||||
Stat = fsedsl.StatOverride[Ctx]
|
||||
UID = fsedsl.UID[Ctx]
|
||||
GID = fsedsl.GID[Ctx]
|
||||
Bind = fsedsl.Bind[Ctx]
|
||||
DirNode = fsedsl.DirNode[Ctx]
|
||||
FileNode = fsedsl.FileNode[Ctx]
|
||||
Each = fsedsl.Each[Ctx]
|
||||
Doc = fsedsl.Doc[Ctx]
|
||||
Read = fsedsl.Read[Ctx]
|
||||
Write = fsedsl.Write[Ctx]
|
||||
// ... add others as needed
|
||||
)
|
||||
|
||||
var Spec = DirNode("/",
|
||||
|
|
@ -169,58 +145,37 @@ var Spec = DirNode("/",
|
|||
return []byte("1.0.0\n"), nil
|
||||
}),
|
||||
),
|
||||
|
||||
DirNode("users",
|
||||
Doc("User management"),
|
||||
Each("{user}", userBindings,
|
||||
FileNode("name", 0444,
|
||||
Read(func(ctx Ctx) ([]byte, error) {
|
||||
return []byte(ctx.User.Name + "\n"), nil
|
||||
}),
|
||||
),
|
||||
Each("{user}", userBindings,
|
||||
FileNode("name", 0444,
|
||||
Read(func(ctx Ctx) ([]byte, error) {
|
||||
return []byte(ctx.User.Name + "\n"), nil
|
||||
}),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
func userBindings(ctx Ctx) ([]Binding, error) {
|
||||
users, err := ctx.DB.ListUsers()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
users, _ := ctx.DB.ListUsers()
|
||||
var out []Binding
|
||||
for _, u := range users {
|
||||
user := u // capture for closure
|
||||
user := u
|
||||
out = append(out, Binding{
|
||||
Name: user.Username,
|
||||
Aliases: []string{user.ID},
|
||||
Applier: func(c Ctx) Ctx {
|
||||
c.User = user
|
||||
return c
|
||||
},
|
||||
Applier: func(c Ctx) Ctx { c.User = user; return c },
|
||||
})
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
```
|
||||
|
||||
### Step 3: Build and use the tree
|
||||
Build and use:
|
||||
|
||||
```go
|
||||
// myproject/main.go
|
||||
package main
|
||||
|
||||
import (
|
||||
"myproject/fs"
|
||||
"git.lneely.de/lkn/fsedsl"
|
||||
)
|
||||
|
||||
func main() {
|
||||
ctx := fs.Ctx{DB: connectDB()}
|
||||
tree := fsedsl.BuildTree(fs.Spec, ctx)
|
||||
|
||||
// Use tree.List(), tree.Open(), tree.Stat(), etc.
|
||||
}
|
||||
tree := fsedsl.BuildTree(fs.Spec, fs.Ctx{DB: db})
|
||||
entries, _ := tree.List()
|
||||
f, _ := tree.Open("alice/name")
|
||||
data, _ := f.Read()
|
||||
```
|
||||
|
||||
## API Reference
|
||||
|
|
|
|||
Loading…
Reference in New Issue