From 5b48eb7ab7587b0c7f174382fbae892f6168c72e Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 Nov 2023 10:18:48 -0400 Subject: [PATCH 1/6] - adds test cleanup after registering default factories Signed-off-by: Vincent Biret --- api_client_builder_test.go | 6 +++++- tests/serialization_writer_factory_registry_test.go | 4 +++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/api_client_builder_test.go b/api_client_builder_test.go index 9d38b84..beada81 100644 --- a/api_client_builder_test.go +++ b/api_client_builder_test.go @@ -2,11 +2,12 @@ package abstractions import ( "fmt" - "github.com/microsoft/kiota-abstractions-go/store" "reflect" "testing" "time" + "github.com/microsoft/kiota-abstractions-go/store" + "github.com/microsoft/kiota-abstractions-go/internal" serialization "github.com/microsoft/kiota-abstractions-go/serialization" assert "github.com/stretchr/testify/assert" @@ -22,6 +23,7 @@ func TestItCreatesClientConcurrently(t *testing.T) { waitTime, _ := time.ParseDuration("100ms") // otherwise the routines might not be completed time.Sleep(waitTime) assert.Equal(t, 1, len(serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories)) + serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) } func TestEnableBackingStoreForSerializationWriterFactory(t *testing.T) { @@ -35,6 +37,7 @@ func TestEnableBackingStoreForSerializationWriterFactory(t *testing.T) { EnableBackingStoreForSerializationWriterFactory(factory) assert.IsType(t, &store.BackingStoreSerializationWriterProxyFactory{}, serializationFactoryRegistry.ContentTypeAssociatedFactories[StreamContentType]) + serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) } func TestEnableBackingStoreForParseNodeFactory(t *testing.T) { @@ -48,6 +51,7 @@ func TestEnableBackingStoreForParseNodeFactory(t *testing.T) { EnableBackingStoreForParseNodeFactory(factory) assert.IsType(t, &store.BackingStoreParseNodeFactory{}, parseNodeRegistry.ContentTypeAssociatedFactories[StreamContentType]) + serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory) } // IsType asserts that the specified objects are of the same type. diff --git a/tests/serialization_writer_factory_registry_test.go b/tests/serialization_writer_factory_registry_test.go index e580cdf..db01a50 100644 --- a/tests/serialization_writer_factory_registry_test.go +++ b/tests/serialization_writer_factory_registry_test.go @@ -1,9 +1,10 @@ package tests import ( - "github.com/microsoft/kiota-abstractions-go/serialization" "testing" + "github.com/microsoft/kiota-abstractions-go/serialization" + "github.com/microsoft/kiota-abstractions-go/internal" assert "github.com/stretchr/testify/assert" ) @@ -13,6 +14,7 @@ func TestItGetsVendorSpecificSerializationWriter(t *testing.T) { serializationWriter, err := serialization.DefaultSerializationWriterFactoryInstance.GetSerializationWriter("application/vnd+json") assert.Nil(t, err) assert.NotNil(t, serializationWriter) + serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) } func TestSerializationWriterFactoryRegistryHonoursInterface(t *testing.T) { From c1d5e08d1935dcdd0fe891ddba3d1634ddff3532 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 Nov 2023 10:38:34 -0400 Subject: [PATCH 2/6] - adds serialization helper methods --- internal/mock_serializer.go | 8 ++- internal/person.go | 14 ++++ kiota_serializer_test.go | 71 +++++++++++++++++++ serialization/kiota_serializer.go | 111 ++++++++++++++++++++++++++++++ 4 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 kiota_serializer_test.go create mode 100644 serialization/kiota_serializer.go diff --git a/internal/mock_serializer.go b/internal/mock_serializer.go index f118d29..efd902b 100644 --- a/internal/mock_serializer.go +++ b/internal/mock_serializer.go @@ -9,6 +9,7 @@ import ( type MockSerializer struct { CallsCounter map[string]int + SerializedValue string } func (m *MockSerializer) WriteNullValue(key string) error { @@ -130,7 +131,10 @@ func (*MockSerializer) WriteCollectionOfTimeOnlyValues(key string, collection [] func (*MockSerializer) WriteCollectionOfUUIDValues(key string, collection []uuid.UUID) error { return nil } -func (*MockSerializer) GetSerializedContent() ([]byte, error) { +func (m *MockSerializer) GetSerializedContent() ([]byte, error) { + if (m.SerializedValue != "") { + return []byte(m.SerializedValue), nil + } return []byte("content"), nil } func (*MockSerializer) WriteAdditionalData(value map[string]interface{}) error { @@ -145,6 +149,7 @@ func (*MockSerializer) Close() error { type MockSerializerFactory struct { serialization.SerializationWriter + SerializedValue string } func (*MockSerializerFactory) GetValidContentType() (string, error) { @@ -154,6 +159,7 @@ func (m *MockSerializerFactory) GetSerializationWriter(contentType string) (seri if m.SerializationWriter == nil { m.SerializationWriter = &MockSerializer{ CallsCounter: make(map[string]int), + SerializedValue: m.SerializedValue, } } return m.SerializationWriter, nil diff --git a/internal/person.go b/internal/person.go index d77037b..c545cf9 100644 --- a/internal/person.go +++ b/internal/person.go @@ -3,6 +3,7 @@ package internal import "github.com/microsoft/kiota-abstractions-go/serialization" type Person struct { + id *string displayName *string callRecord *CallRecord callRecords []*CallRecord @@ -38,6 +39,12 @@ func (c *CallRecord) GetFieldDeserializers() map[string]func(serialization.Parse func NewCallRecord() *CallRecord { return &CallRecord{} } +func (c *Person) SetId(id *string) { + c.id = id +} +func (c *Person) GetId() *string { + return c.id +} func (u *Person) SetDisplayName(name *string) { u.displayName = name } @@ -85,3 +92,10 @@ func (u *Person) SetCardNumbers(numbers []int) { func (u *Person) GetCardNumbers() []int { return u.cardNumbers } +func (c *Person) Serialize(writer serialization.SerializationWriter) error { + panic("implement me") +} + +func (c *Person) GetFieldDeserializers() map[string]func(serialization.ParseNode) error { + panic("implement me") +} diff --git a/kiota_serializer_test.go b/kiota_serializer_test.go new file mode 100644 index 0000000..8d72073 --- /dev/null +++ b/kiota_serializer_test.go @@ -0,0 +1,71 @@ +package abstractions + +import ( + "testing" + + "github.com/microsoft/kiota-abstractions-go/internal" + "github.com/microsoft/kiota-abstractions-go/serialization" + assert "github.com/stretchr/testify/assert" +) + +var jsonContentType = "application/json" + +func TestItDefendsSerializationEmptyContentType(t *testing.T) { + result, err := serialization.Serialize("", nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDefendsSerializationNilValue(t *testing.T) { + result, err := serialization.Serialize(jsonContentType, nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDefendsCollectionSerializationEmptyContentType(t *testing.T) { + result, err := serialization.SerializeCollection("", nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDefendsCollectionSerializationNilValue(t *testing.T) { + result, err := serialization.SerializeCollection(jsonContentType, nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItSerializesObject(t *testing.T) { + serializedValue := "{\"id\":\"123\"}" + metaFactory := func() serialization.SerializationWriterFactory { + return &internal.MockSerializerFactory{ + SerializedValue: serializedValue, + } + } + RegisterDefaultSerializer(metaFactory) + person := internal.NewPerson() + id := "123" + person.SetId(&id) + result, err := serialization.Serialize(jsonContentType, person) + assert.Nil(t, err) + assert.NotNil(t, result) + assert.Equal(t, serializedValue, string(result)) + serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) +} + +func TestItSerializesACollectionOfObjects(t *testing.T) { + serializedValue := "[{\"id\":\"123\"}]" + metaFactory := func() serialization.SerializationWriterFactory { + return &internal.MockSerializerFactory{ + SerializedValue: serializedValue, + } + } + RegisterDefaultSerializer(metaFactory) + person := internal.NewPerson() + id := "123" + person.SetId(&id) + result, err := serialization.SerializeCollection(jsonContentType, []serialization.Parsable{person}) + assert.Nil(t, err) + assert.NotNil(t, result) + assert.Equal(t, serializedValue, string(result)) + serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) +} diff --git a/serialization/kiota_serializer.go b/serialization/kiota_serializer.go new file mode 100644 index 0000000..47643df --- /dev/null +++ b/serialization/kiota_serializer.go @@ -0,0 +1,111 @@ +package serialization + +import ( + "errors" + "reflect" + "strings" +) + +func Serialize(contentType string, model Parsable) ([]byte, error) { + writer, err := getSerializationWriter(contentType, model) + if err != nil { + return nil, err + } + defer writer.Close() + err = writer.WriteObjectValue("", model) + if err != nil { + return nil, err + } + return writer.GetSerializedContent() +} +func SerializeCollection(contentType string, models []Parsable) ([]byte, error) { + writer, err := getSerializationWriter(contentType, models) + if err != nil { + return nil, err + } + defer writer.Close() + err = writer.WriteCollectionOfObjectValues("", models) + if err != nil { + return nil, err + } + return writer.GetSerializedContent() +} +func getSerializationWriter(contentType string, value interface{}) (SerializationWriter, error) { + if contentType == "" { + return nil, errors.New("the content type is empty") + } + if value == nil { + return nil, errors.New("the value is empty") + } + writer, err := DefaultSerializationWriterFactoryInstance.GetSerializationWriter(contentType) + if err != nil { + return nil, err + } + return writer, nil +} + +func Deserialize(contentType string, content []byte, parsableFactory ParsableFactory) (Parsable, error) { + node, err := getParseNode(contentType, content, parsableFactory) + if err != nil { + return nil, err + } + result, err := node.GetObjectValue(parsableFactory) + if err != nil { + return nil, err + } + return result, nil +} +func getParseNode(contentType string, content []byte, parsableFactory ParsableFactory) (ParseNode, error) { + if contentType == "" { + return nil, errors.New("the content type is empty") + } + if content == nil || len(content) == 0 { + return nil, errors.New("the content is empty") + } + if parsableFactory == nil { + return nil, errors.New("the parsable factory is empty") + } + node, err := DefaultParseNodeFactoryInstance.GetRootParseNode(contentType, content) + if err != nil { + return nil, err + } + return node, nil +} +func DeserializeCollection(contentType string, content []byte, parsableFactory ParsableFactory) ([]Parsable, error) { + node, err := getParseNode(contentType, content, parsableFactory) + if err != nil { + return nil, err + } + result, err := node.GetCollectionOfObjectValues(parsableFactory) + if err != nil { + return nil, err + } + return result, nil +} + +func DeserializeWithType(contentType string, content []byte, modelType reflect.Type) (Parsable, error) { + factory, err := getTypeFactory(modelType) + if err != nil { + return nil, err + } + return Deserialize(contentType, content, factory) +} +func DeserializeCollectionWithType(contentType string, content []byte, modelType reflect.Type) ([]Parsable, error) { + factory, err := getTypeFactory(modelType) + if err != nil { + return nil, err + } + return DeserializeCollection(contentType, content, factory) +} + +func getTypeFactory(modelType reflect.Type) (ParsableFactory, error) { + typeName := modelType.Name() + method, ok := modelType.MethodByName("Create" + strings.ToUpper(typeName[0:0]) + typeName[1:] + "FromDiscriminatorValue") + if !ok { + return nil, errors.New("the model type does not have a factory method") + } + factory := func(parseNode ParseNode) (Parsable, error) { + return method.Func.Call([]reflect.Value{reflect.ValueOf(parseNode)})[0].Interface().(Parsable), nil + } + return factory, nil +} From b50f0401e35c87e4d8509999cf0c2ec59cfeeeae Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 Nov 2023 11:24:56 -0400 Subject: [PATCH 3/6] - adds deserialization methods with parsable --- internal/mock_serializer.go | 41 ++++++++++++--- internal/person.go | 3 ++ kiota_serializer_test.go | 84 +++++++++++++++++++++++++++++++ serialization/kiota_serializer.go | 3 ++ 4 files changed, 125 insertions(+), 6 deletions(-) diff --git a/internal/mock_serializer.go b/internal/mock_serializer.go index efd902b..d2b0467 100644 --- a/internal/mock_serializer.go +++ b/internal/mock_serializer.go @@ -8,7 +8,7 @@ import ( ) type MockSerializer struct { - CallsCounter map[string]int + CallsCounter map[string]int SerializedValue string } @@ -132,7 +132,7 @@ func (*MockSerializer) WriteCollectionOfUUIDValues(key string, collection []uuid return nil } func (m *MockSerializer) GetSerializedContent() ([]byte, error) { - if (m.SerializedValue != "") { + if m.SerializedValue != "" { return []byte(m.SerializedValue), nil } return []byte("content"), nil @@ -158,7 +158,7 @@ func (*MockSerializerFactory) GetValidContentType() (string, error) { func (m *MockSerializerFactory) GetSerializationWriter(contentType string) (serialization.SerializationWriter, error) { if m.SerializationWriter == nil { m.SerializationWriter = &MockSerializer{ - CallsCounter: make(map[string]int), + CallsCounter: make(map[string]int), SerializedValue: m.SerializedValue, } } @@ -166,10 +166,39 @@ func (m *MockSerializerFactory) GetSerializationWriter(contentType string) (seri } type MockParseNodeFactory struct { - serialization.ParseNodeFactoryRegistry + serialization.ParseNodeFactory + SerializedValue any } func NewMockParseNodeFactory() *MockParseNodeFactory { - registry := serialization.NewParseNodeFactoryRegistry() - return &MockParseNodeFactory{*registry} + return &MockParseNodeFactory{} +} +func (*MockParseNodeFactory) GetValidContentType() (string, error) { + return "application/json", nil +} + +type MockParseNode struct { + serialization.ParseNode + SerializedValue any +} + +func (m *MockParseNodeFactory) GetRootParseNode(contentType string, content []byte) (serialization.ParseNode, error) { + return &MockParseNode{ + SerializedValue: m.SerializedValue, + }, nil +} + +func (m *MockParseNode) GetObjectValue(ctor serialization.ParsableFactory) (serialization.Parsable, error) { + castValue, ok := m.SerializedValue.(serialization.Parsable) + if ok { + return castValue, nil + } + return ctor(m) +} +func (m *MockParseNode) GetCollectionOfObjectValues(ctor serialization.ParsableFactory) ([]serialization.Parsable, error) { + castValue, ok := m.SerializedValue.([]serialization.Parsable) + if ok { + return castValue, nil + } + return nil, nil } diff --git a/internal/person.go b/internal/person.go index c545cf9..54c35cb 100644 --- a/internal/person.go +++ b/internal/person.go @@ -39,6 +39,9 @@ func (c *CallRecord) GetFieldDeserializers() map[string]func(serialization.Parse func NewCallRecord() *CallRecord { return &CallRecord{} } +func CreatePersonFromDiscriminatorValue(parseNode serialization.ParseNode) (serialization.Parsable, error) { + return &Person{}, nil +} func (c *Person) SetId(id *string) { c.id = id } diff --git a/kiota_serializer_test.go b/kiota_serializer_test.go index 8d72073..774414a 100644 --- a/kiota_serializer_test.go +++ b/kiota_serializer_test.go @@ -69,3 +69,87 @@ func TestItSerializesACollectionOfObjects(t *testing.T) { assert.Equal(t, serializedValue, string(result)) serialization.DefaultSerializationWriterFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.SerializationWriterFactory) } + +func TestItDefendsDeserializationEmptyContentType(t *testing.T) { + result, err := serialization.Deserialize("", nil, nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} +func TestItDefendsDeserializationNilContent(t *testing.T) { + result, err := serialization.Deserialize(jsonContentType, nil, nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} +func TestItDefendsDeserializationNilFactory(t *testing.T) { + result, err := serialization.Deserialize(jsonContentType, make([]byte, 0), nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDefendsCollectionDeserializationEmptyContentType(t *testing.T) { + result, err := serialization.DeserializeCollection("", nil, nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} +func TestItDefendsCollectionDeserializationNilContent(t *testing.T) { + result, err := serialization.DeserializeCollection(jsonContentType, nil, nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} +func TestItDefendsCollectionDeserializationNilFactory(t *testing.T) { + result, err := serialization.DeserializeCollection(jsonContentType, make([]byte, 0), nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDefendsDeserializationNilType(t *testing.T) { + result, err := serialization.DeserializeWithType(jsonContentType, make([]byte, 0), nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDefendsCollectionDeserializationNilType(t *testing.T) { + result, err := serialization.DeserializeCollectionWithType(jsonContentType, make([]byte, 0), nil) + assert.Nil(t, result) + assert.NotNil(t, err) +} + +func TestItDeserializesAnObject(t *testing.T) { + person := internal.NewPerson() + id := "123" + person.SetId(&id) + metaFactory := func() serialization.ParseNodeFactory { + return &internal.MockParseNodeFactory{ + SerializedValue: person, + } + } + RegisterDefaultDeserializer(metaFactory) + + result, err := serialization.Deserialize(jsonContentType, []byte("{\"id\": \"123\"}"), internal.CreatePersonFromDiscriminatorValue) + assert.Nil(t, err) + assert.NotNil(t, result) + resultAsPerson, ok := result.(*internal.Person) + assert.True(t, ok) + assert.Equal(t, id, *resultAsPerson.GetId()) + serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory) +} + +func TestItDeserializesAnObjectCollection(t *testing.T) { + person := internal.NewPerson() + id := "123" + person.SetId(&id) + metaFactory := func() serialization.ParseNodeFactory { + return &internal.MockParseNodeFactory{ + SerializedValue: []serialization.Parsable{person}, + } + } + RegisterDefaultDeserializer(metaFactory) + + result, err := serialization.DeserializeCollection(jsonContentType, []byte("[{\"id\": \"123\"}]"), internal.CreatePersonFromDiscriminatorValue) + assert.Nil(t, err) + assert.NotNil(t, result) + resultAsPerson, ok := result[0].(*internal.Person) + assert.True(t, ok) + assert.Equal(t, id, *resultAsPerson.GetId()) + serialization.DefaultParseNodeFactoryInstance.ContentTypeAssociatedFactories = make(map[string]serialization.ParseNodeFactory) +} diff --git a/serialization/kiota_serializer.go b/serialization/kiota_serializer.go index 47643df..8a8916e 100644 --- a/serialization/kiota_serializer.go +++ b/serialization/kiota_serializer.go @@ -99,6 +99,9 @@ func DeserializeCollectionWithType(contentType string, content []byte, modelType } func getTypeFactory(modelType reflect.Type) (ParsableFactory, error) { + if modelType == nil { + return nil, errors.New("the model type is empty") + } typeName := modelType.Name() method, ok := modelType.MethodByName("Create" + strings.ToUpper(typeName[0:0]) + typeName[1:] + "FromDiscriminatorValue") if !ok { From 56b3c5e9c761f638d7902745ad8b707eacff3bf3 Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 Nov 2023 14:26:14 -0400 Subject: [PATCH 4/6] - cleans up reflection based methods Signed-off-by: Vincent Biret --- kiota_serializer_test.go | 12 ------------ serialization/kiota_serializer.go | 32 ------------------------------- 2 files changed, 44 deletions(-) diff --git a/kiota_serializer_test.go b/kiota_serializer_test.go index 774414a..3262573 100644 --- a/kiota_serializer_test.go +++ b/kiota_serializer_test.go @@ -102,18 +102,6 @@ func TestItDefendsCollectionDeserializationNilFactory(t *testing.T) { assert.NotNil(t, err) } -func TestItDefendsDeserializationNilType(t *testing.T) { - result, err := serialization.DeserializeWithType(jsonContentType, make([]byte, 0), nil) - assert.Nil(t, result) - assert.NotNil(t, err) -} - -func TestItDefendsCollectionDeserializationNilType(t *testing.T) { - result, err := serialization.DeserializeCollectionWithType(jsonContentType, make([]byte, 0), nil) - assert.Nil(t, result) - assert.NotNil(t, err) -} - func TestItDeserializesAnObject(t *testing.T) { person := internal.NewPerson() id := "123" diff --git a/serialization/kiota_serializer.go b/serialization/kiota_serializer.go index 8a8916e..64ac43a 100644 --- a/serialization/kiota_serializer.go +++ b/serialization/kiota_serializer.go @@ -2,8 +2,6 @@ package serialization import ( "errors" - "reflect" - "strings" ) func Serialize(contentType string, model Parsable) ([]byte, error) { @@ -82,33 +80,3 @@ func DeserializeCollection(contentType string, content []byte, parsableFactory P } return result, nil } - -func DeserializeWithType(contentType string, content []byte, modelType reflect.Type) (Parsable, error) { - factory, err := getTypeFactory(modelType) - if err != nil { - return nil, err - } - return Deserialize(contentType, content, factory) -} -func DeserializeCollectionWithType(contentType string, content []byte, modelType reflect.Type) ([]Parsable, error) { - factory, err := getTypeFactory(modelType) - if err != nil { - return nil, err - } - return DeserializeCollection(contentType, content, factory) -} - -func getTypeFactory(modelType reflect.Type) (ParsableFactory, error) { - if modelType == nil { - return nil, errors.New("the model type is empty") - } - typeName := modelType.Name() - method, ok := modelType.MethodByName("Create" + strings.ToUpper(typeName[0:0]) + typeName[1:] + "FromDiscriminatorValue") - if !ok { - return nil, errors.New("the model type does not have a factory method") - } - factory := func(parseNode ParseNode) (Parsable, error) { - return method.Func.Call([]reflect.Value{reflect.ValueOf(parseNode)})[0].Interface().(Parsable), nil - } - return factory, nil -} From 6be9498af91231b5369f5c2a2c37d8cb3a2fe84c Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 Nov 2023 14:30:28 -0400 Subject: [PATCH 5/6] - adds doc comments and json shorthands --- serialization/kiota_json_serializer.go | 23 +++++++++++++++++++++++ serialization/kiota_serializer.go | 6 ++++++ 2 files changed, 29 insertions(+) create mode 100644 serialization/kiota_json_serializer.go diff --git a/serialization/kiota_json_serializer.go b/serialization/kiota_json_serializer.go new file mode 100644 index 0000000..80aa59e --- /dev/null +++ b/serialization/kiota_json_serializer.go @@ -0,0 +1,23 @@ +package serialization + +var jsonContentType = "application/json" + +// SerializeToJson serializes the given model to JSON +func SerializeToJson(model Parsable) ([]byte, error) { + return Serialize(jsonContentType, model) +} + +// SerializeCollectionToJson serializes the given models to JSON +func SerializeCollectionToJson(models []Parsable) ([]byte, error) { + return SerializeCollection(jsonContentType, models) +} + +// DeserializeFromJson deserializes the given JSON to a model +func DeserializeFromJson(content []byte, parsableFactory ParsableFactory) (Parsable, error) { + return Deserialize(jsonContentType, content, parsableFactory) +} + +// DeserializeCollectionFromJson deserializes the given JSON to a collection of models +func DeserializeCollectionFromJson(content []byte, parsableFactory ParsableFactory) ([]Parsable, error) { + return DeserializeCollection(jsonContentType, content, parsableFactory) +} diff --git a/serialization/kiota_serializer.go b/serialization/kiota_serializer.go index 64ac43a..7cc44c6 100644 --- a/serialization/kiota_serializer.go +++ b/serialization/kiota_serializer.go @@ -4,6 +4,7 @@ import ( "errors" ) +// Serialize serializes the given model into a byte array. func Serialize(contentType string, model Parsable) ([]byte, error) { writer, err := getSerializationWriter(contentType, model) if err != nil { @@ -16,6 +17,8 @@ func Serialize(contentType string, model Parsable) ([]byte, error) { } return writer.GetSerializedContent() } + +// SerializeCollection serializes the given models into a byte array. func SerializeCollection(contentType string, models []Parsable) ([]byte, error) { writer, err := getSerializationWriter(contentType, models) if err != nil { @@ -42,6 +45,7 @@ func getSerializationWriter(contentType string, value interface{}) (Serializatio return writer, nil } +// Deserialize deserializes the given byte array into a model. func Deserialize(contentType string, content []byte, parsableFactory ParsableFactory) (Parsable, error) { node, err := getParseNode(contentType, content, parsableFactory) if err != nil { @@ -69,6 +73,8 @@ func getParseNode(contentType string, content []byte, parsableFactory ParsableFa } return node, nil } + +// DeserializeCollection deserializes the given byte array into a collection of models. func DeserializeCollection(contentType string, content []byte, parsableFactory ParsableFactory) ([]Parsable, error) { node, err := getParseNode(contentType, content, parsableFactory) if err != nil { From f3dfa5c0f872bc375c7001f3ed93b195db685cbd Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Wed, 1 Nov 2023 14:31:44 -0400 Subject: [PATCH 6/6] - adds changelog entry for serialization methods --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b7f64ce..2fcf024 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.4.0] - 2023-11-01 + +### Added + +- Added serialization helpers. [microsoft/kiota#3406](https://github.com/microsoft/kiota/issues/3406) + ## [1.3.1] - 2023-10-31 ### Changed