Skip to content

Shimming to an IDENT type #439

@natewernimont-wk

Description

@natewernimont-wk

I'm trying to shim an interface into something that knows how to properly serialize/deserialize that interface. Here is an example:

package foo

//go:generate go tool msgp -io=false -tests=false
//msgp:shim *Bar as:BarSerializer using:ToBarSerializer/unusedFn
//msgp:ignore BarSerializer Bar

type Bar interface{}

type Foo struct {
	Bar
}

type BarSerializer struct {
	Bar *Bar
}

func ToBarSerializer(b *Bar) BarSerializer {
	return BarSerializer{Bar: b}
}

func (b BarSerializer) MarshalMsg(o []byte) ([]byte, error) {
	// actual logic
	return o, nil
}

func (b BarSerializer) UnmarshalMsg(o []byte) ([]byte, error) {
	// actual logic
	*b.Bar = nil // set appropriately
	return o, nil
}

func (b BarSerializer) Msgsize() int {
	return 0
}

Using this generation, the shim is completely unused. I believe it is because the .Alias call here does not set .Convert = true when IDENT types are provided in the as. As such, the shim logic isn't used at all in marshal/unmarshal/size.

Unconditionally setting Convert to true for aliases fixes this problem and properly uses the shim.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions