go-toml icon indicating copy to clipboard operation
go-toml copied to clipboard

Support RawMessage for Partial Field Structs

Open bwagner5 opened this issue 2 years ago • 4 comments

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

  • go-toml: version (git sha) -> github.com/pelletier/go-toml/v2 v2.0.2 (216628222f5716163bb96d3651a68feef31b090d)
  • go: version -> go version go1.18.1 darwin/amd64
  • operating system: e.g. macOS, Windows, Linux -> macOS

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

bwagner5 avatar Jul 08 '22 05:07 bwagner5

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

pelletier avatar Jul 10 '22 02:07 pelletier

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

bwagner5 avatar Jul 10 '22 16:07 bwagner5

Any updates?

shizhx avatar Mar 18 '24 13:03 shizhx

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

pelletier avatar Mar 19 '24 16:03 pelletier