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

Error unmarshalling sync list json #358

Closed
fideloper opened this issue Jul 13, 2022 · 1 comment · Fixed by #361
Closed

Error unmarshalling sync list json #358

fideloper opened this issue Jul 13, 2022 · 1 comment · Fixed by #361
Assignees
Labels

Comments

@fideloper
Copy link

Using structs from here: https://github.com/mutagen-io/mutagen/tree/v0.15.0/pkg/api/models (specifically the Session struct) I was attempting to unmarshall the results of calling mutagen sync list --format "{{ json . }}".

I received error json: cannot unmarshal bool into Go struct field .ignore.vcs of type core.IgnoreVCSMode

Here's the info and steps to reproduce:

  • Mutagen 0.15.0
  • Mac OS Monterey
import (
	"encoding/json"
	"fmt"
	"github.com/mutagen-io/mutagen/pkg/api/models/synchronization"
	"os/exec"
	"strings"
)

func ListSyncs() error {
	exe, err := exec.LookPath(process.ExecutableName("mutagen"))

	if err != nil {
		return fmt.Errorf("unable to determine mutagen path: %w", err)
	}

	list := &exec.Cmd{
		Path: exe,
		Args: []string{
			exe,
			"sync", "list",
			"--template", `"{{ json . }}"`,
		},
	}

	output, err := list.CombinedOutput()

	if err != nil {
		if exitError, ok := err.(*exec.ExitError); ok {
			return fmt.Errorf("mutagen sync error: %s \n %s", exitError.String(), output)
		} else {
			return fmt.Errorf("unable to list mutagen sync sessions: %w", err)
		}
	}

	// 
	sessions := make([]synchronization.Session, 0)

	// This was also kind of crazy, I had to trim characters around the output to get this to unmarshall 
	// (convert to string, trim characters, convert back to byte array!)
	err = json.Unmarshal([]byte(strings.Trim(string(output), "\n\"")), &sessions)

	fmt.Println(err)
	fmt.Println(sessions)
}

json returned gives true or false but this struct wants an int32 with values 0, 1, or 2 (looks like it’s protobuf-generated object)
10:25

Lastly, from our quick Slack convo:

image

@xenoscopic xenoscopic self-assigned this Jul 13, 2022
@xenoscopic xenoscopic added the bug label Jul 13, 2022
xenoscopic added a commit that referenced this issue Aug 8, 2022
It turns out that the encoding/json package will only invoke
UnmarshalText if the JSON value is a quoted string. Since we're dealing
with boolean literals, we need to add an explicit UnmarshalJSON method,
but we can still use UnmarshalText for the implementation.

Fixes #358.

Signed-off-by: Jacob Howard <jacob@mutagen.io>
xenoscopic added a commit that referenced this issue Aug 8, 2022
It turns out that the encoding/json package will only invoke
UnmarshalText if the JSON value is a quoted string. Since we're dealing
with boolean literals, we need to add an explicit UnmarshalJSON method,
but we can still use UnmarshalText for the implementation.

Fixes #358.

Signed-off-by: Jacob Howard <jacob@mutagen.io>
xenoscopic added a commit that referenced this issue Aug 8, 2022
It turns out that the encoding/json package will only invoke
UnmarshalText if the JSON value is a quoted string. Since we're dealing
with boolean literals, we need to add an explicit UnmarshalJSON method,
but we can still use UnmarshalText for the implementation.

Fixes #358.

Signed-off-by: Jacob Howard <jacob@mutagen.io>
@xenoscopic
Copy link
Member

This should be fixed with the v0.15.1 release. Let me know if the decoding issues persist.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants