Skip to content

Commit

Permalink
Merge pull request #130 from microsoft/andrueastman/untypedNodes
Browse files Browse the repository at this point in the history
[Go] [Serialization] Untyped Nodes
  • Loading branch information
andrueastman authored Mar 20, 2024
2 parents 00f8585 + 9ffafc2 commit d1231c8
Show file tree
Hide file tree
Showing 8 changed files with 492 additions and 27 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.7] - 2024-02-29

### Added

- Adds support for serialization and deserialization untyped nodes.

## [1.0.6] - 2024-02-12

### Changed
Expand Down
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ go 1.18

require (
github.com/google/uuid v1.6.0
github.com/microsoft/kiota-abstractions-go v1.5.6
github.com/stretchr/testify v1.8.4
github.com/microsoft/kiota-abstractions-go v1.6.0
github.com/stretchr/testify v1.9.0
)

require (
Expand All @@ -15,9 +15,9 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/std-uritemplate/std-uritemplate/go v0.0.50 // indirect
go.opentelemetry.io/otel v1.22.0 // indirect
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
github.com/std-uritemplate/std-uritemplate/go v0.0.55 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
24 changes: 12 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,20 @@ github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/microsoft/kiota-abstractions-go v1.5.6 h1:3hd1sACWB2B9grv8KG1T8g/gGQ4A8kTLv91OUxHSxkE=
github.com/microsoft/kiota-abstractions-go v1.5.6/go.mod h1:2WX7Oh8V9SAdZ80OGeE53rcbdys54Pd38rAeDUghrpM=
github.com/microsoft/kiota-abstractions-go v1.6.0 h1:qbGBNMU0/o5myKbikCBXJFohVCFrrpx2cO15Rta2WyA=
github.com/microsoft/kiota-abstractions-go v1.6.0/go.mod h1:7YH20ZbRWXGfHSSvdHkdztzgCB9mRdtFx13+hrYIEpo=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/std-uritemplate/std-uritemplate/go v0.0.50 h1:LAE6WYRmLlDXPtEzr152BnD/MHxGCKmcp5D2Pw0NvmU=
github.com/std-uritemplate/std-uritemplate/go v0.0.50/go.mod h1:CLZ1543WRCuUQQjK0BvPM4QrG2toY8xNZUm8Vbt7vTc=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
go.opentelemetry.io/otel v1.22.0 h1:xS7Ku+7yTFvDfDraDIJVpw7XPyuHlB9MCiqqX5mcJ6Y=
go.opentelemetry.io/otel v1.22.0/go.mod h1:eoV4iAi3Ea8LkAEI9+GFT44O6T/D0GWAVFyZVCC6pMI=
go.opentelemetry.io/otel/metric v1.22.0 h1:lypMQnGyJYeuYPhOM/bgjbFM6WE44W1/T45er4d8Hhg=
go.opentelemetry.io/otel/metric v1.22.0/go.mod h1:evJGjVpZv0mQ5QBRJoBF64yMuOf4xCWdXjK8pzFvliY=
go.opentelemetry.io/otel/trace v1.22.0 h1:Hg6pPujv0XG9QaVbGOBVHunyuLcCC3jN7WEhPx83XD0=
go.opentelemetry.io/otel/trace v1.22.0/go.mod h1:RbbHXVqKES9QhzZq/fE5UnOSILqRt40a21sPw2He1xo=
github.com/std-uritemplate/std-uritemplate/go v0.0.55 h1:muSH037g97K7U2f94G9LUuE8tZlJsoSSrPsO9V281WY=
github.com/std-uritemplate/std-uritemplate/go v0.0.55/go.mod h1:rG/bqh/ThY4xE5de7Rap3vaDkYUT76B0GPJ0loYeTTc=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
176 changes: 176 additions & 0 deletions internal/untyped_test_entity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package internal

import (
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)

type UntypedTestEntity struct {
additionalData map[string]interface{}
id *string
title *string
location absser.UntypedNodeable
keywords absser.UntypedNodeable
detail absser.UntypedNodeable
}

type TestUntypedTestEntityable interface {
absser.Parsable
absser.AdditionalDataHolder
GetId() *string
SetId(value *string)
GetTitle() *string
SetTitle(value *string)
GetLocation() absser.UntypedNodeable
SetLocation(value absser.UntypedNodeable)
GetKeywords() absser.UntypedNodeable
SetKeywords(value absser.UntypedNodeable)
GetDetail() absser.UntypedNodeable
SetDetail(value absser.UntypedNodeable)
}

func NewUntypedTestEntity() *UntypedTestEntity {
return &UntypedTestEntity{
additionalData: make(map[string]interface{}),
}
}

func UntypedTestEntityDiscriminator(parseNode absser.ParseNode) (absser.Parsable, error) {
return NewUntypedTestEntity(), nil
}

func (e *UntypedTestEntity) GetAdditionalData() map[string]interface{} {
return e.additionalData
}
func (e *UntypedTestEntity) SetAdditionalData(value map[string]interface{}) {
e.additionalData = value
}

func (e *UntypedTestEntity) GetId() *string {
return e.id
}
func (e *UntypedTestEntity) SetId(value *string) {
e.id = value
}

func (e *UntypedTestEntity) GetTitle() *string {
return e.title
}
func (e *UntypedTestEntity) SetTitle(value *string) {
e.title = value
}

func (e *UntypedTestEntity) GetLocation() absser.UntypedNodeable {
return e.location
}
func (e *UntypedTestEntity) SetLocation(value absser.UntypedNodeable) {
e.location = value
}

func (e *UntypedTestEntity) GetKeywords() absser.UntypedNodeable {
return e.keywords
}
func (e *UntypedTestEntity) SetKeywords(value absser.UntypedNodeable) {
e.keywords = value
}

func (e *UntypedTestEntity) GetDetail() absser.UntypedNodeable {
return e.detail
}
func (e *UntypedTestEntity) SetDetail(value absser.UntypedNodeable) {
e.detail = value
}

func (e *UntypedTestEntity) GetFieldDeserializers() map[string]func(absser.ParseNode) error {
res := make(map[string]func(absser.ParseNode) error)
res["id"] = func(n absser.ParseNode) error {
val, err := n.GetStringValue()
if err != nil {
return err
}
if val != nil {
e.SetId(val)
}
return nil
}
res["title"] = func(n absser.ParseNode) error {
val, err := n.GetStringValue()
if err != nil {
return err
}
if val != nil {
e.SetTitle(val)
}
return nil
}
res["location"] = func(n absser.ParseNode) error {
val, err := n.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return err
}
if val != nil {
e.SetLocation(val.(absser.UntypedNodeable))
}
return nil
}
res["keywords"] = func(n absser.ParseNode) error {
val, err := n.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return err
}
if val != nil {
e.SetKeywords(val.(absser.UntypedNodeable))
}
return nil
}
res["detail"] = func(n absser.ParseNode) error {
val, err := n.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return err
}
if val != nil {
e.SetDetail(val.(absser.UntypedNodeable))
}
return nil
}
return res
}

func (m *UntypedTestEntity) Serialize(writer absser.SerializationWriter) error {
{
err := writer.WriteStringValue("id", m.GetId())
if err != nil {
return err
}
}
{
err := writer.WriteStringValue("title", m.GetTitle())
if err != nil {
return err
}
}
{
err := writer.WriteObjectValue("location", m.GetLocation())
if err != nil {
return err
}
}
{
err := writer.WriteObjectValue("keywords", m.GetKeywords())
if err != nil {
return err
}
}
{
err := writer.WriteObjectValue("detail", m.GetDetail())
if err != nil {
return err
}
}
{
err := writer.WriteAdditionalData(m.GetAdditionalData())
if err != nil {
return err
}
}
return nil
}
59 changes: 58 additions & 1 deletion json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/google/uuid"
"io"
"time"

"github.com/google/uuid"

abstractions "github.com/microsoft/kiota-abstractions-go"
absser "github.com/microsoft/kiota-abstractions-go/serialization"
)
Expand Down Expand Up @@ -187,6 +188,62 @@ func (n *JsonParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Pars
if err != nil {
return nil, err
}

_, isUntypedNode := result.(absser.UntypedNodeable)
if isUntypedNode {
switch value := n.value.(type) {
case *bool:
return absser.NewUntypedBoolean(*value), nil
case *string:
return absser.NewUntypedString(*value), nil
case *float32:
return absser.NewUntypedFloat(*value), nil
case *float64:
return absser.NewUntypedDouble(*value), nil
case *int32:
return absser.NewUntypedInteger(*value), nil
case *int64:
return absser.NewUntypedLong(*value), nil
case nil:
return absser.NewUntypedNull(), nil
case map[string]*JsonParseNode:
properties := make(map[string]absser.UntypedNodeable)
for key, value := range value {
parsable, err := value.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return nil, errors.New("cannot parse object value")
}
if parsable == nil {
parsable = absser.NewUntypedNull()
}
property, ok := parsable.(absser.UntypedNodeable)
if ok {
properties[key] = property
}
}
return absser.NewUntypedObject(properties), nil
case []*JsonParseNode:
collection := make([]absser.UntypedNodeable, len(value))
for index, node := range value {
parsable, err := node.GetObjectValue(absser.CreateUntypedNodeFromDiscriminatorValue)
if err != nil {
return nil, errors.New("cannot parse object value")
}
if parsable == nil {
parsable = absser.NewUntypedNull()
}
property, ok := parsable.(absser.UntypedNodeable)
if ok {
collection[index] = property
}

}
return absser.NewUntypedArray(collection), nil
default:
return absser.NewUntypedNode(value), nil
}
}

abstractions.InvokeParsableAction(n.GetOnBeforeAssignFieldValues(), result)
properties, ok := n.value.(map[string]*JsonParseNode)
fields := result.GetFieldDeserializers()
Expand Down
Loading

0 comments on commit d1231c8

Please sign in to comment.