我在 go 中有以下PartnerType“enum”结构:
// PartnerType enum
type PartnerType int
const (
// PartnerTypeUnknown should never happen
PartnerTypeUnknown PartnerType = iota
// PartnerTypeTest test
PartnerTypeTest
)
func (t PartnerType) String() string {
names := [...]string{
"UNKNOWN",
"TEST"}
if t < PartnerTypeUnknown || t > PartnerTypeTest {
return names[0]
}
return names[t]
}
// MarshalJSON for enum
func (t PartnerType) MarshalJSON() ([]byte, error) {
s := t.String()
return json.Marshal(s)
}
// UnmarshalJSON for enum
func (t *PartnerType) UnmarshalJSON(b []byte) error {
*t = PartnerTypeUnknown
return nil
}
// MarshalText implements the text marshaller method
func (t *PartnerType) MarshalText() ([]byte, error) {
return []byte(t.String()), nil
}
// UnmarshalText implements the text unmarshaller method
func (t *PartnerType) UnmarshalText(text []byte) error {
*t = PartnerTypeUnknown
return nil
}
// Partner entity
type Partner struct {
ID string `dynamodbav:"PK,omitempty"`
Foo string `dynamodbav:"SK,omitempty"`
PartnerType PartnerType `dynamodbav:",omitempty,string"`
}
在执行 a 时,dynamodbattribute.MarshalMap()我希望PartnerType将 my 存储为字符串。dynamodbav结构标记允许您将 int 存储为字符串 ( ) ,dynamodbav:",omitempty,string"但它不会调用我定义的.String()或Marshal/Unmarshal实现。
换句话说,它存储PartnerTypeTest为"1"not "TEST"。
如何才能做到这一点?
四季花海
相关分类