pelletier / go-toml

Go library for the TOML file format
https://github.com/pelletier/go-toml
Other
1.69k stars 206 forks source link

Support RawMessage for Partial Field Structs #796

Open bwagner5 opened 2 years ago

bwagner5 commented 2 years ago

Describe the bug Feature Request (or question if already possible):

I'd like to be able to parse a struct w/ partial fields defined and have go-toml v2 Unmarshal into the fields defined in the struct and store the rest of the TOML into a []byte. The go json pkg has this capability by embedding the json.RawMessage type (a []byte) on the struct.

To Reproduce Steps to reproduce the behavior. Including TOML files.

https://go.dev/play/p/5KE5CeRhXPB

package main

import (
    "fmt"
    "os"

    "github.com/pelletier/go-toml/v2"
)

type RawMessage []byte
type Doc struct {
    Test struct {
        Key string `toml:"key"`
        RawMessage
    } `toml:"test"`
}

func main() {
    d := Doc{}
    if err := toml.Unmarshal([]byte(`
         [test]
         key = 'test'
         undefined-key = 'not here'
        `), &d); err != nil {
        fmt.Printf("error :( -> %v", err)
        os.Exit(1)
    }
    fmt.Printf("%+v", d)
}

output:

panic: reflect: NumField of non-struct type main.RawMessage
...

Expected behavior A clear and concise description of what you expected to happen, if other than "should work".

The output of the snippet above should be something like this:

{Test:{Key:test, RawMessage: undefined-key = 'not here'}}

Versions

Additional context Add any other context about the problem here that you think may help to diagnose.

pelletier commented 2 years ago

Neat idea! Do you think there would be any functional difference with json.RawMessage?

bwagner5 commented 2 years ago

I can't think of any cases that would deviate from the way json.RawMessage works.

shizhx commented 5 months ago

Any updates?

pelletier commented 5 months ago

@shizhx I don't think anyone is working on this at the moment.