From d087200e02660589a9f774ed98d442c641ade792 Mon Sep 17 00:00:00 2001 From: Cory Slep Date: Thu, 3 Jan 2019 22:56:19 +0100 Subject: [PATCH] Implement type's Serialize function --- tools/exp/props/type.go | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/tools/exp/props/type.go b/tools/exp/props/type.go index e45346b..26efe8b 100644 --- a/tools/exp/props/type.go +++ b/tools/exp/props/type.go @@ -497,6 +497,24 @@ func (t *TypeGenerator) disjointWithDefinition() *codegen.Function { // serializationMethod returns the method needed to serialize a TypeGenerator as // a property. func (t *TypeGenerator) serializationMethod() (ser *codegen.Method) { + serCode := jen.Empty() + for _, prop := range t.allProperties() { + serCode.Add( + jen.Commentf("Maybe serialize property %q", prop.PropertyName()).Line(), + jen.If( + jen.List( + jen.Id("i"), + jen.Err(), + ).Op(":=").Id(codegen.This()).Dot(t.memberName(prop)).Dot(serializeMethod).Call(), + jen.Err().Op("!=").Nil(), + ).Block( + jen.Return(jen.Nil(), jen.Err()), + ).Else().If( + jen.Id("i").Op("!=").Nil(), + ).Block( + jen.Id("m").Index(jen.Id(codegen.This()).Dot(t.memberName(prop)).Dot(nameMethod).Call()).Op("=").Id("i"), + ).Line()) + } ser = codegen.NewCommentedValueMethod( t.PrivatePackage().Path(), serializeMethodName, @@ -504,8 +522,11 @@ func (t *TypeGenerator) serializationMethod() (ser *codegen.Method) { /*params=*/ nil, []jen.Code{jen.Interface(), jen.Error()}, []jen.Code{ - // TODO - jen.Commentf("TODO: Serialization code for %s", t.TypeName()), + jen.Id("m").Op(":=").Make( + jen.Map(jen.String()).Interface(), + ), + serCode, + jen.Return(jen.Id("m"), jen.Nil()), }, jen.Commentf("%s converts this into an interface representation suitable for marshalling into a text or binary format.", serializeMethodName)) return