I have a file with many types of data record which I need to parse into structs.
I'd be grateful to learn of a idiomatic way -- if it exists -- of filling structs by record type. Something like python's namedtuple(*fields)
constructor.
package main
import (
"fmt"
"strconv"
"strings"
)
type X interface{}
type HDR struct {
typer, a string
b int
}
type BDY struct {
typer, c string
d int
e string
}
var lines string = `HDR~two~5
BDY~four~6~five`
func sn(s string) int {
i, _ := strconv.Atoi(s)
return i
}
func main() {
sl := strings.Split(lines, "\n")
for _, l := range sl {
fields := strings.Split(l, "~")
var r X
switch fields[0] {
case "HDR":
r = HDR{fields[0], fields[1], sn(fields[2])} // 1
case "BDY":
r = BDY{fields[0], fields[1], sn(fields[2]), fields[3]} // 2
}
fmt.Printf("%T : %v\n", r, r)
}
}
I'm specifically interested to learn if lines marked // 1
and // 2
can be conveniently replaced by code, perhaps some sort of generic decoder which allows the struct itself to handle type conversion.
Use the reflect package to programmatically set fields.
A field must be exported to be set by the reflect package. Export the names by uppercasing the first rune in the name:
Create a map of names to the type associated with the name:
For each line, create a value of the type using the
types
map:Loop over the fields in the line. Get the field value, convert the string to the kind of the field value and set the field value:
This is a basic outline of the approach. Error handling is notabling missing: the application will panic if the type name is not one of the types mentioned in
types
; the application ignores the error returned from parsing the integer; the application will panic if there are more fields in the data than the struct; the application does not report an error when it encounters an unsupported field kind; and more.Run it on the Go Playground.