file

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Codec

type Codec[T any] struct {
	// contains filtered or unexported fields
}

Codec encodes T to a file and decodes T from a file using an underlying Codec[T]. Writes are atomic: data is written to a temporary file and renamed into place. It is safe for concurrent use.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/foomo/goencode/file"
	json "github.com/foomo/goencode/json/v1"
)

func main() {
	type Data struct {
		Name string
	}

	c := file.NewCodec(json.NewCodec[Data]())

	dir, err := os.MkdirTemp("", "file-codec-example")
	if err != nil {
		fmt.Printf("TempDir failed: %v\n", err)
		return
	}
	defer os.RemoveAll(dir)

	path := filepath.Join(dir, "data.json")

	if err := c.Encode(path, Data{Name: "example-123"}); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded Data
	if err := c.Decode(path, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Name: %s\n", decoded.Name)
}
Output:
Decoded Name: example-123

func NewCodec

func NewCodec[T any](codec encoding.Codec[T], opts ...Option) *Codec[T]

NewCodec returns a file codec that delegates serialization to codec.

func (*Codec[T]) Decode

func (c *Codec[T]) Decode(path string, v *T) error

Decode reads the file at path and deserializes its contents into v.

func (*Codec[T]) Encode

func (c *Codec[T]) Encode(path string, v T) error

Encode serializes v and atomically writes the result to path.

type Option

type Option func(o *options)

Option configures a file Codec.

func WithPermissions

func WithPermissions(perm os.FileMode) Option

WithPermissions sets the file permissions for written files (default: 0o644).

type StreamCodec

type StreamCodec[T any] struct {
	// contains filtered or unexported fields
}

StreamCodec encodes T to a file and decodes T from a file using an underlying StreamCodec[T]. Writes are atomic: data is written to a temporary file and renamed into place. It is safe for concurrent use.

Example
package main

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/foomo/goencode/file"
	json "github.com/foomo/goencode/json/v1"
)

func main() {
	type Data struct {
		Name string
	}

	c := file.NewStreamCodec(json.NewStreamCodec[Data]())

	dir, err := os.MkdirTemp("", "file-streamcodec-example")
	if err != nil {
		fmt.Printf("TempDir failed: %v\n", err)
		return
	}
	defer os.RemoveAll(dir)

	path := filepath.Join(dir, "data.json")

	if err := c.Encode(path, Data{Name: "example-123"}); err != nil {
		fmt.Printf("Encode failed: %v\n", err)
		return
	}

	var decoded Data
	if err := c.Decode(path, &decoded); err != nil {
		fmt.Printf("Decode failed: %v\n", err)
		return
	}

	fmt.Printf("Decoded Name: %s\n", decoded.Name)
}
Output:
Decoded Name: example-123

func NewStreamCodec

func NewStreamCodec[T any](codec encoding.StreamCodec[T], opts ...Option) *StreamCodec[T]

NewStreamCodec returns a file stream codec that delegates serialization to codec.

func (*StreamCodec[T]) Decode

func (c *StreamCodec[T]) Decode(path string, v *T) error

Decode reads the file at path and deserializes its contents into v.

func (*StreamCodec[T]) Encode

func (c *StreamCodec[T]) Encode(path string, v T) error

Encode serializes v and atomically writes the result to path.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL