https://github.com/clickermonkey/refstr
https://github.com/clickermonkey/refstr
Last synced: 8 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/clickermonkey/refstr
- Owner: ClickerMonkey
- Created: 2022-12-20T01:45:08.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2023-01-20T04:26:23.000Z (about 3 years ago)
- Last Synced: 2025-01-27T10:23:08.486Z (about 1 year ago)
- Language: Go
- Size: 13.7 KB
- Stars: 0
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# refstr
A go module for converting between strings and any value AND referencing a path from a value through its structure to an end value and getting and setting it.
**String Examples**:
```go
// A value already initialized
x := 0
e := refstr.Decode(&x, "45")
// Specify a type and string
v, e := refstr.DecodeType(refstr.TypeOf[bool](), "yes")
// A value not yet initialized
var y *int
e := refstr.Decode(&y, "56")
// A slice
var z []float32
e := refstr.Decode(&z, "[0.5 1 3.1415]")
// A map
var w map[string]int
e := refstr.Decode(&w, "map[A:1 B:2 C:3]")
// A struct
type Point { X, Y float32 }
var p Point
e := refstr.Decode(&p, "{X:4 Y:67}")
// Control how types are parsed further with your own decoder
dec := refstr.NewDecoder()
dec.Trues["yeppers"] = struct{}{}
var b bool
e := dec.Decode(&b, "yeppers")
```
With references you can follow a path of fields, maps, slice & array elements, and getter and setter functions to get and set a value. Once a set is done it can create all the elements in the path if they don't exist yet.
**Reference Examples**:
```go
type Name struct { First, Last string }
func (n Name) Full() string { return n.Last + ", " + n.First }
type Person struct { Name Name }
type Persons struct { ByName map[string]Person }
p := Persons{}
pref := refstr.NewRef(&p)
// references John's name in the map, but none of it exists yet.
johnName := pref.Nexts([]any{"ByName", "John", "Name"})
// creates john in the map and sets his first name
johnName.Next("First").Set("John")
// sets his last name
johnName.Next("Last").Set("Doe")
// gets his full name from the method Full()
full, _ := johnName.Next("Full").Get()
```