Documentation
¶
Overview ¶
Package docs reads and writes a workspace's files for AutoDoc's own apps. A Docs works over one workspace root, given as a golib vfs.FS; it never opens a file any other way.
A read returns the content with the version it was read at: the file is stat'ed before and after the read, and read again when the version moved, so the version names the bytes returned. Every write goes through the vfs, conditional on the version the writer read (a create on the path not existing yet), so the index learns of it the same way it learns of an external edit, and no writer replaces what it has not seen. A path the workspace does not index is refused (ErrNotEligible), and so is a document over MaxSize (ErrTooLarge).
A derived format (a PDF, a DOCX: a file the build's deriver turns into text) reads as its derived text, cut to fit one read when it is longer (TruncatedMark), and is read-only (ErrReadOnly). ReadRaw and WriteRaw reach the file's own bytes of a derived format whose source is text (HTML).
Concurrency: New sets a Docs's fields and no method changes them; a Docs holds no lock and starts no goroutine. Its methods may be called from any number of goroutines when the vfs.FS, the eligible function, the WithTextExtensions function and the WithDeriver table's deriver are safe for concurrent use. Two writers of one path are kept apart by the vfs.FS's conditional operations (CreateExclusive, WriteFileIf, RenameNoReplace, RemoveIf), not by Docs.
Index ¶
- Constants
- Variables
- type Doc
- type Docs
- func (d *Docs) Admits(path string) bool
- func (d *Docs) Derived(path string) bool
- func (d *Docs) Read(ctx context.Context, path string) (Doc, error)
- func (d *Docs) ReadRaw(ctx context.Context, path string) (Doc, error)
- func (d *Docs) Remove(ctx context.Context, path string, want vfs.Version) error
- func (d *Docs) Rename(ctx context.Context, from, to string) error
- func (d *Docs) Write(ctx context.Context, path string, content []byte, want vfs.Version) (vfs.Version, error)
- func (d *Docs) WriteRaw(ctx context.Context, path string, content []byte, want vfs.Version) (vfs.Version, error)
- type Option
Constants ¶
const MaxSize = 4<<20 - 64<<10
MaxSize is the largest document read or written: the RPC message limit (4 MiB), less room for the message around it. A larger file is refused, never truncated. A derived document's text is the one exception: it is cut to fit, and says so (Read).
const TruncatedMark = "[autodoc: truncated]"
TruncatedMark begins the last line of a derived text cut to fit one read. The line is AutoDoc's, never the document's: a reader quoting the text stops before it.
Variables ¶
var ( // ErrCommitted is a write that landed, then a follow-up step failed (Committed, -32066): the // change took effect but no version can be promised. The client reads the document and compares; // it never sends the write again. ErrCommitted = errs.Sentinel(errs.ErrPrecondition, "docs: the write landed, then a follow-up step failed") // ErrNotEligible is a path the workspace does not index (outside include, inside exclude): // AutoDoc's apps edit files, not every file under the root. ErrNotEligible = errs.Sentinel(errs.ErrInvalidArgument, "docs: not a file of this workspace") // ErrTooLarge is a document over MaxSize, or a derived document's file over derived.MaxContainer. ErrTooLarge = errs.Sentinel(errs.ErrInvalidArgument, "docs: the document is over the size limit") // ErrReadOnly is a write, a rename or a removal of a derived document: AutoDoc serves its // text, and never changes the file it was made of. ErrReadOnly = errs.Sentinel(errs.ErrInvalidArgument, "docs: a derived document is read-only") )
Functions ¶
This section is empty.
Types ¶
type Docs ¶
type Docs struct {
// contains filtered or unexported fields
}
Docs is the document API over one workspace root.
func (*Docs) Admits ¶ added in v0.1.25
Admits reports whether path is a document of these: eligible, and a format the build reads.
func (*Docs) Derived ¶ added in v0.1.15
Derived reports whether path is a format the build derives: Read returns text derived from the file, not the file's own bytes.
func (*Docs) Read ¶
Read returns the document at path with the version its content was read at. The file is stat'ed before and after the read, and read again when it changed in between, so the version never names other bytes than the ones returned.
func (*Docs) ReadRaw ¶ added in v0.1.32
ReadRaw is Read without deriving: the file's own bytes and their version, for a derived format whose source is text (kind.RawText: HTML), so it can be edited. Any other document reads as Read reads it; any other derived format is refused, whatever its bytes.
func (*Docs) Write ¶
func (d *Docs) Write(ctx context.Context, path string, content []byte, want vfs.Version) (vfs.Version, error)
Write writes content to path if the document is still at version want, and returns the new version. want "" creates a document that must not exist yet, and the folders above it. A stale want, or an existing path for a create, fails with a *vfs.ConflictError and leaves the file as it is.
func (*Docs) WriteRaw ¶ added in v0.1.32
func (d *Docs) WriteRaw(ctx context.Context, path string, content []byte, want vfs.Version) (vfs.Version, error)
WriteRaw is Write that admits a derived format whose source is text (kind.RawText: HTML), with text bytes: the file's own bytes, edited. Any other derived format is refused before anything is written, whatever the version: an existing PDF is never replaced.
type Option ¶ added in v0.1.9
type Option func(*Docs)
Option configures Docs.
func WithDeriver ¶ added in v0.1.13
func WithDeriver(reg *registrations.Table) Option
WithDeriver reads the formats reg derives as their derived text, read-only.
func WithRegistrations ¶ added in v0.1.12
func WithRegistrations(r kind.Registrations) Option
WithRegistrations reads files of the build's registered extensions as their kind (kind.Registered: UTF-8 text, validated as such on a write).
func WithTextExtensions ¶ added in v0.1.9
WithTextExtensions reads the workspace's own plain-text extensions (core/kind) from text, asked at each read and write, so a change applies without a restart.