versioned

package
v1.1.4 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

versioned module

The versioned package provides utilities to manage versioned data streams and files. It uses the blang/semver package to handle semantic versioning.

Error Handling

ErrNotVersioned: Error raised if the underlying reader has no version information.

IsNotVersioned: A function checks if an error is caused by a "not versioned" stream.

Structs

Reader: Represents a versioned reader that can load the version of the data from a stream.

Fields
  • Reader io.Reader: The underlying data stream.
  • version Version: The version of the data.
Methods

Version: Returns the version of the data.

Functions

NewVersionedReader: Creates a Reader with a specified version.

NewReader: Creates a Reader by reading the version from a stream. Fails if the stream lacks version information.

ReadFile: Reads the content of a file and its version.

NewWriter: Creates a versioned writer that marks data with the provided version.

WriteFile: Writes versioned data to a file.

Parse: Parses a version string into a Version object.

MustParse: Parses a version string into a Version object. Panics if the parsing fails.

ParseRange: Parses a version range string into a Range object.

MustParseRange: Parses a version range string into a Range object. Panics if the parsing fails.

Documentation

Overview

Example
type DataV1 struct {
	FullName string `json:"name"`
}

type DataV2 struct {
	FirstName string `json:"first_name"`
	LastName  string `json:"last_name"`
}

//latest := MustParse("1.2.0")
// 1- Open file contains data
bits := `{"name": "John Smith"}`

buf := bytes.NewBufferString(bits)
// 2- create versioned reader
reader, err := NewReader(buf)
if IsNotVersioned(err) {
	reader = NewVersionedReader(MustParse("1.0.0"), bytes.NewBufferString(bits))
} else if err != nil {
	panic(err)
}

dec := json.NewDecoder(reader)
var data DataV2 // final object
var resave bool
if reader.Version().EQ(MustParse("1.0.0")) { //V1 object
	// data migration from v1 to v2
	var d1 DataV1
	if err := dec.Decode(&d1); err != nil {
		panic(err)
	}
	parts := strings.SplitN(d1.FullName, " ", 2)
	data = DataV2{
		FirstName: parts[0],
		LastName:  parts[1],
	}
	resave = true
} else if reader.Version().EQ(MustParse("2.0.0")) { //V2 (current) version
	if err := dec.Decode(&data); err != nil {
		panic(err)
	}
} else {
	panic("unknown version")
}

if resave {
	var buf bytes.Buffer
	writer, _ := NewWriter(&buf, MustParse("2.0.0"))
	enc := json.NewEncoder(writer)
	if err := enc.Encode(data); err != nil {
		panic(err)
	}

	fmt.Println(buf.String())
}
Output:
"2.0.0"{"first_name":"John","last_name":"Smith"}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotVersioned error is raised if the underlying reader has no version
	ErrNotVersioned = fmt.Errorf("no version information")
)

Functions

func IsNotVersioned

func IsNotVersioned(err error) bool

IsNotVersioned checks if error is caused by a 'not versioned' stream

func NewWriter

func NewWriter(w io.Writer, version Version) (io.Writer, error)

NewWriter creates a versioned writer that marks data with the Provided version.

func WriteFile

func WriteFile(filename string, version Version, data []byte, perm os.FileMode) error

WriteFile versioned data to file

Types

type Range

type Range = semver.Range

Range type

func MustParseRange

func MustParseRange(s string) Range

MustParseRange parses range

func ParseRange

func ParseRange(s string) (Range, error)

ParseRange parses a range

type Reader

type Reader struct {
	io.Reader
	// contains filtered or unexported fields
}

Reader is a versioned reader The Versioned Reader is a reader that can load the version of the data from a stream without assuming anything regarding the underlying encoding of your data object

func NewReader

func NewReader(r io.Reader) (*Reader, error)

NewReader creates a new versioned reader from a stream. It fails if the reader can not read the version from the stream. On success, the reader will have a version, and then can be used to load the data.

Example
latest := MustParse("1.2.0")
// 1- Open file contains data
buf := bytes.NewBufferString(`"1.0.1" "my data goes here"`)

// 2- create versioned reader
reader, err := NewReader(buf)
if err != nil {
	// no version in data, take another action!
	panic(err)
}

fmt.Println("data version is:", reader.Version())
dec := json.NewDecoder(reader)

if reader.Version().Compare(latest) <= 0 {
	//data version is older than or equal latest
	var data string
	if err := dec.Decode(&data); err != nil {
		panic(err)
	}
	fmt.Println("data is:", "my data goes here")
}
Output:
data version is: 1.0.1
data is: my data goes here

func NewVersionedReader

func NewVersionedReader(version Version, r io.Reader) *Reader

NewVersionedReader creates a versioned reader from an un-versioned reader. It's usually used to unify the data migration work flow in case the older data file didn't have a version stamp example:

 reader, err := NewReader(file)
 if IsNotVersioned(err) {
     file.Seek(0, 0) // this is important to make u reading from start
     reader = NewVersionedReader(MustParse("0.0.0"), file)
 } else err != nil {
   // probably io error
}

func (*Reader) Version

func (r *Reader) Version() Version

Version return the version of the data

type Version

type Version = semver.Version

Version type

func MustParse

func MustParse(s string) Version

MustParse version

func Parse

func Parse(s string) (Version, error)

Parse version string

func ReadFile

func ReadFile(path string) (Version, []byte, error)

ReadFile content

Jump to

Keyboard shortcuts

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