dataer

command module
v0.0.0-...-97fee5c Latest Latest
Warning

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

Go to latest
Published: Jul 6, 2024 License: MIT Imports: 7 Imported by: 0

README

lesiw.io/dataer

An experimental package to add metadata to types.

Specify a type with -t T and the data to extend it with as -d D. The type will be made to fulfill the interface:

type Dataer interface {
    Data() D // Return value is the type chosen to extend T.
}

Example: Struct

//go:generate go run lesiw.io/dataer@latest -t superstring -d stringdata

package main

import "fmt"
    
type superstring string

type stringdata struct {
    String string
    Int int
}

func main() {
    x := superstring("a string")
    y := superstring("a string")
    x.Data().String = "x"
    x.Data().Int = 42
    y.Data().String = "y"
    fmt.Println(x)
    fmt.Println("x == y?", x == y)
    fmt.Println("x str:", x.Data().String)
    fmt.Println("y str:", y.Data().String)
    fmt.Println("x int:", x.Data().Int)
    fmt.Println("y int:", y.Data().Int)
}

Example: Basic type

//go:generate go run lesiw.io/dataer@latest -t superstring -d int

package main

import "fmt"

type superstring string

func main() {
	x := superstring("a string")
	y := superstring("a string")
	*x.Data() = 42
	*y.Data() = -1
	fmt.Println(x)
	fmt.Println("x == y?", x == y)
	fmt.Println("x int:", *x.Data())
	fmt.Println("y int:", *y.Data())
}

Documentation

Overview

Package dataer adds a metadata type to another type.

Jump to

Keyboard shortcuts

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