如何为AWS ION编写自定义的解组器(unmarshaller)?

huangapple go评论85阅读模式
英文:

How to write a custom unmarshaller for AWS ION?

问题

我正在使用Amazon ION来对接收自各种AWS服务的数据进行编组和解组。

我需要编写一个自定义的解组函数,并在Amazon ION的官方文档中找到了一个实现方法的示例,请参见这里

使用上述示例,我编写了以下代码:

package main

import (
	"bytes"
	"fmt"

	"github.com/amzn/ion-go/ion"
)

func main() {
	UnmarshalCustomMarshaler()
}

type unmarshalMe struct {
	Name   string
	custom bool
}

func (u *unmarshalMe) UnmarshalIon(r ion.Reader) error {
	fmt.Print("UnmarshalIon called")
	u.custom = true
	return nil
}

func UnmarshalCustomMarshaler() {
	ionBinary, err := ion.MarshalBinary(unmarshalMe{
		Name: "John Doe",
	})
	if err != nil {
		fmt.Println("Error marshalling ion binary: ", err)
		panic(err)
	}

	dec := ion.NewReader(bytes.NewReader(ionBinary))
	var decodedResult unmarshalMe

	ion.UnmarshalFrom(dec, &decodedResult)
	fmt.Println("Decoded result: ", decodedResult)
}

问题: 上述代码不按预期工作。UnmarshalIon函数没有被调用,但根据文档应该会被调用。我做错了什么?

英文:

I'm using Amazon ION to marshal and unmarshal data received from various AWS services.

I need to write a custom unmarshal function, and I found an example of how thats achieved in Amazon ION's official docs, see here

Using the above example, I have written below code:

package main

import (
	"bytes"
	"fmt"

	"github.com/amzn/ion-go/ion"
)

func main() {
	UnmarshalCustomMarshaler()
}

type unmarshalMe struct {
	Name   string
	custom bool
}

func (u *unmarshalMe) UnmarshalIon(r ion.Reader) error {
	fmt.Print("UnmarshalIon called")
	u.custom = true
	return nil
}

func UnmarshalCustomMarshaler() {
	ionBinary, err := ion.MarshalBinary(unmarshalMe{
		Name: "John Doe",
	})
	if err != nil {
		fmt.Println("Error marshalling ion binary: ", err)
		panic(err)
	}

	dec := ion.NewReader(bytes.NewReader(ionBinary))
	var decodedResult unmarshalMe

	ion.UnmarshalFrom(dec, &decodedResult)
	fmt.Println("Decoded result: ", decodedResult)
}

Problem: The above code does not work as expected. The UnmarshalIon function is not called, but according to docs is should get called. What am I doing wrong?

答案1

得分: 1

你可能正在使用的是默认的v1.1.3版本,该版本不包含该功能。

英文:

You are probably using the v1.1.3, the one by default which does not contain the feature.

huangapple
  • 本文由 发表于 2023年1月4日 20:23:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/75005399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定