Skip to content

reduce the amount of code being generated #26

Closed
microsoft/kiota
#1788

Description

@baywet

The build time of the service SDKs can take a while for the initial build/dependencies upgrade on some machines.
This is caused by the breadth of our API but we might have opportunities to improve that aspect by reducing the amount of code we generate.

The get field deserializers method might have opportunities for reduction by moving some of the code into helper methods in abstractions
https://github.com/microsoftgraph/msgraph-sdk-go/blob/3a362de6cb79ffc639841e0d8fb122975173c145/models/microsoft/graph/message.go#L320

Same for the serialize method
https://github.com/microsoftgraph/msgraph-sdk-go/blob/3a362de6cb79ffc639841e0d8fb122975173c145/models/microsoft/graph/message.go#L667

Lastly the field accessors might also provide such opportunity
https://github.com/microsoftgraph/msgraph-sdk-go/blob/3a362de6cb79ffc639841e0d8fb122975173c145/models/microsoft/graph/message.go#L160

The goal here is to look into these patterns, identify and share the examples of what could be improved, so we can feed the information back to the kiota generation process.

Activity

  1. added this to the GA milestone on Jan 14, 2022
  2. baywet commented on Aug 15, 2022

    @baywet
    MemberAuthor

    Accessors

    Currently accessors are generated this way which is unnecessarily verbose:

    func (m *Message) GetCcRecipients()([]Recipientable) {
    -    if m == nil {
    -        return nil
    -    } else {
            return m.ccRecipients
    -    }
    }

    If application calls the accessor on a nil object, it actually should get an error/panic since it's probably in an invalid state at this point and this is what we'd do in other languages.

    Same for setters

    func (m *Message) SetCcRecipients(value []Recipientable)() {
    -    if m != nil {
            m.ccRecipients = value
    -    }
    }

    Serializers

    Since writing scalars and objects writing only returns an error, we can probably reduce the block by doing

    -{
    -        err = writer.WriteObjectValue("body", m.GetBody())
    -        if err != nil {
    +        if err := writer.WriteObjectValue("body", m.GetBody()); err != nil {
                return err
            }
    -}

    For collections of objects, we might be able to do away with the casting like so

    if m.GetBccRecipients() != nil {
    -        cast := make([]i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable, len(m.GetBccRecipients()))
    -        for i, v := range m.GetBccRecipients() {
    -            cast[i] = v.(i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable)
    -        }
    -        err = writer.WriteCollectionOfObjectValues("bccRecipients", cast)
    +        err = writer.WriteCollectionOfObjectValues("bccRecipients", m.GetBccRecipients())
            if err != nil {
                return err
            }
        }

    Checking for nil remains important since a nil collection and an empty collection have different semantic meanings.

    And regardless, we should implement the error assignment shorthand mentioned above for scalars/objects.

    Deserializers

    After looking into that further, I don't believe we can achieve significant reductions in deserializers without using generics which is outside of this scope.

  3. changed the title [-]build time improvements[/-] [+]reduce the amount of code being generated[/+] on Aug 16, 2022
  4. rkodev commented on Aug 16, 2022

    @rkodev
    Contributor

    Vincent Biret (@baywet) For the collections its not possible to directly type case then as this is failing to comply

    err = writer.WriteCollectionOfObjectValues("bccRecipients", m.GetBccRecipients())

    However, we can use generics to convert this function as well

    func convert[T any, S any](collection []T) []S {
    	cast := make([]S, len(collection))
    	for i, v := range collection {
    		cast[i] = any(v).(S)
    	}
    	return cast
    }
    
    		casted := convert[iadcd81124412c61e647227ecfc4449d8bba17de0380ddda76f641a29edf2b242.AssignedLicenseable,i878a80d2330e89d26896388a3f487eef27b0a0e6c010c493bf80be1452208f91.Parsable](m.GetAddLicenses())
    		err := writer.WriteCollectionOfObjectValues("addLicenses", casted)
  5. baywet commented on Aug 16, 2022

    @baywet
    MemberAuthor

    Thanks for looking into that. I'm assuming you're getting an error like []Recipientable is not []Parsable correct? seems like covariance could improved in Go...

    I'm a bit reluctant to introduce this convert method right now since we're planning to take advantage of generics. Hopefully we can make the WriteCollectionOfObjectValues covariant in microsoft/kiota#1404 but let's keep this as a backup plan if we can't.

    So ignore the collections change for the scope of this issue and proceed with the other ones, we'll close this when the other ones are implemented.

  6. rkodev commented on Aug 16, 2022

    @rkodev
    Contributor

    Vincent Biret (@baywet) Yes, that was the error. Skipped it for now, we can consider it for the generics refactor.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementNew feature or request

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions