package jsoncode ¶
import "golazy.dev/lazycode/jsoncode"
Functions ¶
func Dependency ¶
Dependency returns an operation that sets one package.json dependency.
func Dependency(name, group, dependency, version string) lazycode.Operation
func Edit ¶
Edit returns an operation that parses name, applies edit, and replaces the file in memory only when it changed.
func Edit(name string, edit EditFunc) lazycode.Operation
func Set ¶
Set returns an operation that assigns value at an object-key path in name.
func Set(name string, path []string, value any) lazycode.Operation
Types ¶
type Document ¶
Document is a parsed JSON object with its detected indentation and final newline style.
type Document struct {
// contains filtered or unexported fields
}
func Parse ¶
Parse parses exactly one JSON object from data.
func Parse(data []byte) (*Document, error)
func (d *Document) Bytes ¶
Bytes encodes the document using its detected indentation and final newline.
func (d *Document) Bytes() ([]byte, error)
func (d *Document) EnsureDependency ¶
EnsureDependency sets name to version in a supported package.json dependency group.
func (d *Document) EnsureDependency(group, name, version string) (bool, error)
func (d *Document) Remove ¶
Remove deletes the value at an object-key path and reports whether it existed.
func (d *Document) Remove(path []string) (bool, error)
func (d *Document) RemoveDependency ¶
RemoveDependency removes name from a supported package.json dependency group.
func (d *Document) RemoveDependency(group, name string) (bool, error)
func (d *Document) Set ¶
Set assigns value at an object-key path, creating intermediate objects when needed, and reports whether the document changed.
func (d *Document) Set(path []string, value any) (bool, error)
type EditFunc ¶
EditFunc mutates a Document and reports whether it changed.
type EditFunc func(*Document) (bool, error)
Package jsoncode provides structured JSON object edits as lazycode operations.
For example, an installer can plan a package dependency update:
workspace, err := lazycode.Load(".", "package.json") if err != nil { return err } _, err = workspace.Plan( jsoncode.Dependency( "package.json", "dependencies", "@hotwired/turbo", "8.0.13", ), ) if err != nil { return err } if result.Changed() { fmt.Println("package.json would change") }JSON indentation and the final newline are preserved. Object key order is normalized through encoding/json.