bigquery-emulator icon indicating copy to clipboard operation
bigquery-emulator copied to clipboard

Properly unmarshal JSON values into golang values when inserting table data

Open ohaibbq opened this issue 1 year ago • 5 comments

Closes #286 Closes #334 Closes #144 Closes #389 Closes #426

ohaibbq avatar Jul 09 '24 00:07 ohaibbq

I have the same issue.

Workaround meanwhile we do

func decodePayload(payload string, v any) error {
	err := decodePayloadBigquery(payload, v)
	if err != nil {
		return decodePayloadFromEmulator(payload, &v)
	}

	return nil
}

func decodePayloadBigquery(payload string, v any) error {
	err := json.Unmarshal([]byte(payload), &v)
	if err != nil {
		return fmt.Errorf("failed to unmarshal payload from bigquery: %w", err)
	}

	return nil
}

func decodePayloadFromEmulator(payload string, v any) error {
	payload, err := strconv.Unquote(payload)
	if err != nil {
		return fmt.Errorf("failed to unquote payload: %w", err)
	}

	err = json.Unmarshal([]byte(payload), v)
	if err != nil {
		return fmt.Errorf("failed to unmarshal payload from emulator: %w", err)
	}

	return nil
}

renevall avatar Nov 06 '24 22:11 renevall

@goccy mind reviewing this one for @renevall? Hope you're well!

ohaibbq avatar Nov 06 '24 22:11 ohaibbq

If anyone's interested in a plug-in solution, I've used this fix and rebased it with the current repo and packaged it into a docker image: https://github.com/eduhenke/bigquery-emulator/pkgs/container/bigquery-emulator. Just use ghcr.io/eduhenke/bigquery-emulator:0.6.7

eduhenke avatar May 13 '25 12:05 eduhenke

Guess we're still waiting on the status of this, any updates? @goccy

themanifold avatar Jun 30 '25 12:06 themanifold

This has been merged and released in the Recidiviz fork (which includes many other updates) https://github.com/Recidiviz/bigquery-emulator/releases/tag/v0.6.6-recidiviz.0

ohaibbq avatar Nov 10 '25 17:11 ohaibbq