Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support RawMessage for Partial Field Structs #796

Open
bwagner5 opened this issue Jul 8, 2022 · 4 comments
Open

Support RawMessage for Partial Field Structs #796

bwagner5 opened this issue Jul 8, 2022 · 4 comments
Labels
feature Issue asking for a new feature in go-toml.

Comments

@bwagner5
Copy link

bwagner5 commented Jul 8, 2022

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.

@pelletier pelletier added the feature Issue asking for a new feature in go-toml. label Jul 10, 2022
@pelletier
Copy link
Owner

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

@bwagner5
Copy link
Author

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

@shizhx
Copy link

shizhx commented Mar 18, 2024

Any updates?

@pelletier
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Issue asking for a new feature in go-toml.
Projects
None yet
Development

No branches or pull requests

3 participants