Skip to content

Standardize Model Property Accessors #234

@michaeldcanady

Description

@michaeldcanady

Problem Statement

Currently, many models (e.g., FileModel in attachment-api/file.go) manually implement Get methods for every property, leading to significant boilerplate for type conversion and BackingStore interaction. While a generic solution exists in internal/store (DefaultBackedModelAccessorFunc), it is not yet universally adopted across the SDK.

Current Implementation Status

We have implemented:

  • internal/store.DefaultBackedModelAccessorFunc[S, T]

These are successfully used in several models, but older models like FileModel still use manual GetBackingStore().Get calls with explicit type assertions and error handling.

Proposed Solution

  1. Adopt Generic Accessors SDK-wide: Refactor all existing models to use the generic accessor functions from internal/store.
  2. Rename for Brevity (Optional): Consider aliasing these to shorter names like ModelAccessor as originally proposed to improve readability.
  3. Consistency: Ensure all models inherit from a common base (like internal.BaseModel) to guarantee BackingStore availability.

Example Refactor (FileModel)

From:

func (f *FileModel) GetSysID() (*string, error) {
    val, err := f.GetBackingStore().Get(sysIDKey)
    if err != nil {
        return nil, err
    }
    if val == nil {
        return nil, nil
    }
    res, ok := val.(*string)
    if !ok {
        return nil, errors.New("type mismatch")
    }
    return res, nil
}

To:

func (f *FileModel) GetSysID() (*string, error) {
    return store.DefaultBackedModelAccessorFunc[kiotaStore.BackingStore, *string](f.GetBackingStore(), sysIDKey)
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions