Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/generic helpers #34

Merged
merged 8 commits into from
Sep 26, 2022
Merged

Feat/generic helpers #34

merged 8 commits into from
Sep 26, 2022

Conversation

rkodev
Copy link
Contributor

@rkodev rkodev commented Sep 21, 2022

Overview

Partially address microsoft/kiota#1404

Adds generic helper methods for serializers and deserializers

Demo

SetValue
Current

    res["proof"] = func (n i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.ParseNode) error {
        val, err := n.GetStringValue()
        if err != nil {
            return err
        }
        if val != nil {
            m.SetProof(val)
        }
        return nil
    }

Is transformed to

	res["proof"] = msgraphsdkgo.SetStringValue(m.SetProof)
}

SetObjectValue

Current

    res["passwordCredential"] = func (n serialization.ParseNode) error {
        val, err := n.GetObjectValue(iadcd81124412c61e647227ecfc4449d8bba17de0380ddda76f641a29edf2b242.CreatePasswordCredentialFromDiscriminatorValue)
        if err != nil {
            return err
        }
        if val != nil {
            m.SetPasswordCredential(val.(iadcd81124412c61e647227ecfc4449d8bba17de0380ddda76f641a29edf2b242.PasswordCredentialable))
        }
        return nil
    }

Final

	res["passwordCredential"] =  msgraphsdkgo.SetObjectValue(n.GetObjectValue, iadcd81124412c61e647227ecfc4449d8bba17de0380ddda76f641a29edf2b242.CreatePasswordCredentialFromDiscriminatorValue, m.SetPasswordCredential)

SetCollectionValue
From

	res["value"] = func(n serialization.ParseNode) error {
		val, err := n.GetCollectionOfObjectValues(CreatePinnedChatMessageInfoFromDiscriminatorValue)
		if err != nil {
			return err
		}
		if val != nil {
			res := make([]PinnedChatMessageInfoable, len(val))
			for i, v := range val {
				res[i] = v.(PinnedChatMessageInfoable)
			}
			m.SetValue(res)
		}
		return nil
	}

To

	res["value"] = msgraphsdkgo.SetCollectionValue[PinnedChatMessageInfoable](n.GetCollectionOfObjectValues, CreatePinnedChatMessageInfoFromDiscriminatorValue, m.SetValue)

Collection Apply

From

        cast := make([]i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable, len(m.GetValue()))
        for i, v := range m.GetValue() {
            cast[i] = v.(i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable)
        }

To

		cast := msgraphsdkgo.CollectionApply(m.GetValue(), func(v PinnedChatMessageInfoable) i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable {
			return v.(i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable)
		})

Copy link
Member

@baywet baywet left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

holding on the discussions from the samples PR. Also, don't we need to bump the version of go to 19 in the go mod file to make sure the runtime will support generics?

@rkodev
Copy link
Contributor Author

rkodev commented Sep 23, 2022

@baywet Generics were introduced in go 1.18 and we are already on it so I believe it should be fine as it

utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
utils.go Outdated Show resolved Hide resolved
@rkodev rkodev enabled auto-merge (squash) September 26, 2022 12:42
@rkodev rkodev merged commit a8ae4a6 into main Sep 26, 2022
@rkodev rkodev deleted the feat/generic-helpers branch September 26, 2022 13:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants