-
Notifications
You must be signed in to change notification settings - Fork 13
Add Support for User Attributes API to Schema. #72
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| package api | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/google/uuid" | ||
| "github.com/permitio/permit-golang/pkg/config" | ||
| "github.com/permitio/permit-golang/pkg/errors" | ||
| "github.com/permitio/permit-golang/pkg/models" | ||
| "github.com/permitio/permit-golang/pkg/openapi" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // DefaultUserAttributeResourceID is the default resource_id query parameter for user schema attributes (V2 API). | ||
| const DefaultUserAttributeResourceID = "__user" | ||
|
|
||
| type UserAttributes struct { | ||
| permitBaseApi | ||
| } | ||
|
|
||
| func NewUserAttributesApi(client *openapi.APIClient, config *config.PermitConfig) *UserAttributes { | ||
| return &UserAttributes{ | ||
| permitBaseApi: permitBaseApi{ | ||
| client: client, | ||
| config: config, | ||
| logger: config.Logger, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| // List returns all user attributes for the User resource (resource_id __user). | ||
| // | ||
| // attrs, err := PermitClient.Api.UserAttributes.List(ctx, 1, 30) | ||
| func (a *UserAttributes) List(ctx context.Context, page int, perPage int) ([]models.UserAttributeRead, error) { | ||
| return a.ListForResource(ctx, DefaultUserAttributeResourceID, page, perPage) | ||
| } | ||
|
|
||
| // ListForResource lists user attributes when using a non-default user resource id. | ||
| func (a *UserAttributes) ListForResource(ctx context.Context, resourceID string, page int, perPage int) ([]models.UserAttributeRead, error) { | ||
| perPageLimit := int32(DefaultPerPageLimit) | ||
| if !isPaginationInLimit(int32(page), int32(perPage), perPageLimit) { | ||
| err := errors.NewPermitPaginationError() | ||
| a.logger.Error("error listing user attributes - max per page exceeded", zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| err := a.lazyLoadPermitContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| attrs, _, err := a.client.UserAttributesApi.ListUserAttributes(ctx, a.config.Context.GetProject(), a.config.Context.GetEnvironment()). | ||
| ResourceId(resourceID).Page(int32(page)).PerPage(int32(perPage)).Execute() | ||
| if err != nil { | ||
| a.logger.Error("error listing user attributes", zap.Error(err)) | ||
| return nil, err | ||
| } | ||
|
Comment on lines
+50
to
+55
|
||
| return attrs, nil | ||
| } | ||
|
|
||
| // Get returns a user attribute by id or key (slug). | ||
| // | ||
| // attr, err := PermitClient.Api.UserAttributes.Get(ctx, "clearance_level") | ||
| func (a *UserAttributes) Get(ctx context.Context, attributeIDOrKey string) (*models.UserAttributeRead, error) { | ||
| return a.GetForResource(ctx, DefaultUserAttributeResourceID, attributeIDOrKey) | ||
| } | ||
|
|
||
| // GetForResource gets a user attribute with an explicit resource_id query. | ||
| func (a *UserAttributes) GetForResource(ctx context.Context, resourceID, attributeIDOrKey string) (*models.UserAttributeRead, error) { | ||
| err := a.lazyLoadPermitContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| attr, _, err := a.client.UserAttributesApi.GetUserAttribute(ctx, a.config.Context.GetProject(), a.config.Context.GetEnvironment(), attributeIDOrKey). | ||
| ResourceId(resourceID).Execute() | ||
| if err != nil { | ||
| a.logger.Error("error getting user attribute: "+attributeIDOrKey, zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| return attr, nil | ||
| } | ||
|
|
||
| // GetByKey is an alias for Get (attribute key is the slug). | ||
| func (a *UserAttributes) GetByKey(ctx context.Context, attributeKey string) (*models.UserAttributeRead, error) { | ||
| return a.Get(ctx, attributeKey) | ||
| } | ||
|
|
||
| // GetById gets a user attribute by attribute UUID (uses default user resource __user). | ||
| func (a *UserAttributes) GetById(ctx context.Context, attributeID uuid.UUID) (*models.UserAttributeRead, error) { | ||
| return a.Get(ctx, attributeID.String()) | ||
| } | ||
|
|
||
| // Create adds a user schema attribute. | ||
| // | ||
| // create := models.NewUserAttributeCreate("department", models.STRING) | ||
| // create.SetDescription("User department") | ||
| // attr, err := PermitClient.Api.UserAttributes.Create(ctx, *create) | ||
| func (a *UserAttributes) Create(ctx context.Context, body models.UserAttributeCreate) (*models.UserAttributeRead, error) { | ||
| return a.CreateForResource(ctx, DefaultUserAttributeResourceID, body) | ||
| } | ||
|
|
||
| // CreateForResource creates a user attribute with an explicit resource_id query. | ||
| func (a *UserAttributes) CreateForResource(ctx context.Context, resourceID string, body models.UserAttributeCreate) (*models.UserAttributeRead, error) { | ||
| err := a.lazyLoadPermitContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| attr, _, err := a.client.UserAttributesApi.CreateUserAttribute(ctx, a.config.Context.GetProject(), a.config.Context.GetEnvironment()). | ||
| UserAttributeCreate(body).ResourceId(resourceID).Execute() | ||
| if err != nil { | ||
| a.logger.Error("error creating user attribute: "+body.GetKey(), zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| return attr, nil | ||
| } | ||
|
|
||
| // Update partially updates a user attribute (id or key). | ||
| func (a *UserAttributes) Update(ctx context.Context, attributeIDOrKey string, body models.UserAttributeUpdate) (*models.UserAttributeRead, error) { | ||
| return a.UpdateForResource(ctx, DefaultUserAttributeResourceID, attributeIDOrKey, body) | ||
| } | ||
|
|
||
| // UpdateForResource updates with an explicit resource_id query. | ||
| func (a *UserAttributes) UpdateForResource(ctx context.Context, resourceID, attributeIDOrKey string, body models.UserAttributeUpdate) (*models.UserAttributeRead, error) { | ||
| err := a.lazyLoadPermitContext(ctx) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| attr, _, err := a.client.UserAttributesApi.UpdateUserAttribute(ctx, a.config.Context.GetProject(), a.config.Context.GetEnvironment(), attributeIDOrKey). | ||
| UserAttributeUpdate(body).ResourceId(resourceID).Execute() | ||
| if err != nil { | ||
| a.logger.Error("error updating user attribute: "+attributeIDOrKey, zap.Error(err)) | ||
| return nil, err | ||
| } | ||
| return attr, nil | ||
| } | ||
|
|
||
| // Delete removes a user attribute (id or key). | ||
| func (a *UserAttributes) Delete(ctx context.Context, attributeIDOrKey string) error { | ||
| return a.DeleteForResource(ctx, DefaultUserAttributeResourceID, attributeIDOrKey) | ||
| } | ||
|
|
||
| // DeleteForResource deletes with an explicit resource_id query. | ||
| func (a *UserAttributes) DeleteForResource(ctx context.Context, resourceID, attributeIDOrKey string) error { | ||
| err := a.lazyLoadPermitContext(ctx) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| _, err = a.client.UserAttributesApi.DeleteUserAttribute(ctx, a.config.Context.GetProject(), a.config.Context.GetEnvironment(), attributeIDOrKey). | ||
| ResourceId(resourceID).Execute() | ||
| if err != nil { | ||
| a.logger.Error("error deleting user attribute: "+attributeIDOrKey, zap.Error(err)) | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package models | ||
|
|
||
| import "encoding/json" | ||
|
|
||
| // UserAttributeCreate is the request body for creating a user (schema) attribute. | ||
| // It matches the V2 POST /v2/schema/{proj_id}/{env_id}/users/attributes payload. | ||
| type UserAttributeCreate struct { | ||
| // A URL-friendly name of the attribute (i.e: slug). | ||
| Key string `json:"key"` | ||
| // The type of the attribute (bool, number, string, time, array, json, object, object_array). | ||
| Type AttributeType `json:"type"` | ||
| // An optional longer description of what this attribute represents in your system. | ||
| Description *string `json:"description,omitempty"` | ||
| } | ||
|
|
||
| // NewUserAttributeCreate builds a create request with required key and type. | ||
| func NewUserAttributeCreate(key string, type_ AttributeType) *UserAttributeCreate { | ||
| return &UserAttributeCreate{Key: key, Type: type_} | ||
| } | ||
|
|
||
| // NewUserAttributeCreateWithDefaults instantiates an empty UserAttributeCreate. | ||
| func NewUserAttributeCreateWithDefaults() *UserAttributeCreate { | ||
| return &UserAttributeCreate{} | ||
| } | ||
|
|
||
| func (o *UserAttributeCreate) GetKey() string { | ||
| if o == nil { | ||
| return "" | ||
| } | ||
| return o.Key | ||
| } | ||
|
|
||
| func (o *UserAttributeCreate) SetKey(v string) { | ||
| o.Key = v | ||
| } | ||
|
|
||
| func (o *UserAttributeCreate) GetType() AttributeType { | ||
| if o == nil { | ||
| return "" | ||
| } | ||
| return o.Type | ||
| } | ||
|
|
||
| func (o *UserAttributeCreate) SetType(v AttributeType) { | ||
| o.Type = v | ||
| } | ||
|
|
||
| func (o *UserAttributeCreate) GetDescription() string { | ||
| if o == nil || IsNil(o.Description) { | ||
| return "" | ||
| } | ||
| return *o.Description | ||
| } | ||
|
|
||
| func (o *UserAttributeCreate) SetDescription(v string) { | ||
| o.Description = &v | ||
| } | ||
|
|
||
| func (o UserAttributeCreate) MarshalJSON() ([]byte, error) { | ||
| m := map[string]interface{}{"key": o.Key, "type": o.Type} | ||
| if !IsNil(o.Description) { | ||
| m["description"] = o.Description | ||
| } | ||
| return json.Marshal(m) | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing The existing Suggestion: Add func (o *UserAttributeCreate) HasDescription() bool {
return o != nil && !IsNil(o.Description)
} |
||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,75 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| package models | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "encoding/json" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "time" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| // UserAttributeRead is a user schema attribute as returned by the V2 API. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| type UserAttributeRead struct { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Type AttributeType `json:"type"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Description *string `json:"description,omitempty"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Key string `json:"key"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Id string `json:"id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ResourceId string `json:"resource_id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ResourceKey string `json:"resource_key"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| OrganizationId string `json:"organization_id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ProjectId string `json:"project_id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| EnvironmentId string `json:"environment_id"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| CreatedAt time.Time `json:"created_at"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| UpdatedAt time.Time `json:"updated_at"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| BuiltIn bool `json:"built_in"` | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func NewUserAttributeReadWithDefaults() *UserAttributeRead { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return &UserAttributeRead{} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (o *UserAttributeRead) GetKey() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if o == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return o.Key | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (o *UserAttributeRead) GetId() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if o == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return o.Id | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (o *UserAttributeRead) GetType() AttributeType { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if o == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return o.Type | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (o *UserAttributeRead) GetBuiltIn() bool { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if o == nil { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return false | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return o.BuiltIn | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (o *UserAttributeRead) GetDescription() string { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if o == nil || IsNil(o.Description) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return "" | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return *o.Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| func (o UserAttributeRead) MarshalJSON() ([]byte, error) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| m := map[string]interface{}{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "type": o.Type, "key": o.Key, "id": o.Id, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "resource_id": o.ResourceId, "resource_key": o.ResourceKey, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "organization_id": o.OrganizationId, "project_id": o.ProjectId, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "environment_id": o.EnvironmentId, "created_at": o.CreatedAt, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| "updated_at": o.UpdatedAt, "built_in": o.BuiltIn, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if !IsNil(o.Description) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| m["description"] = o.Description | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return json.Marshal(m) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+3
to
+75
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| import ( | |
| "encoding/json" | |
| "time" | |
| ) | |
| // UserAttributeRead is a user schema attribute as returned by the V2 API. | |
| type UserAttributeRead struct { | |
| Type AttributeType `json:"type"` | |
| Description *string `json:"description,omitempty"` | |
| Key string `json:"key"` | |
| Id string `json:"id"` | |
| ResourceId string `json:"resource_id"` | |
| ResourceKey string `json:"resource_key"` | |
| OrganizationId string `json:"organization_id"` | |
| ProjectId string `json:"project_id"` | |
| EnvironmentId string `json:"environment_id"` | |
| CreatedAt time.Time `json:"created_at"` | |
| UpdatedAt time.Time `json:"updated_at"` | |
| BuiltIn bool `json:"built_in"` | |
| } | |
| func NewUserAttributeReadWithDefaults() *UserAttributeRead { | |
| return &UserAttributeRead{} | |
| } | |
| func (o *UserAttributeRead) GetKey() string { | |
| if o == nil { | |
| return "" | |
| } | |
| return o.Key | |
| } | |
| func (o *UserAttributeRead) GetId() string { | |
| if o == nil { | |
| return "" | |
| } | |
| return o.Id | |
| } | |
| func (o *UserAttributeRead) GetType() AttributeType { | |
| if o == nil { | |
| return "" | |
| } | |
| return o.Type | |
| } | |
| func (o *UserAttributeRead) GetBuiltIn() bool { | |
| if o == nil { | |
| return false | |
| } | |
| return o.BuiltIn | |
| } | |
| func (o *UserAttributeRead) GetDescription() string { | |
| if o == nil || IsNil(o.Description) { | |
| return "" | |
| } | |
| return *o.Description | |
| } | |
| func (o UserAttributeRead) MarshalJSON() ([]byte, error) { | |
| m := map[string]interface{}{ | |
| "type": o.Type, "key": o.Key, "id": o.Id, | |
| "resource_id": o.ResourceId, "resource_key": o.ResourceKey, | |
| "organization_id": o.OrganizationId, "project_id": o.ProjectId, | |
| "environment_id": o.EnvironmentId, "created_at": o.CreatedAt, | |
| "updated_at": o.UpdatedAt, "built_in": o.BuiltIn, | |
| } | |
| if !IsNil(o.Description) { | |
| m["description"] = o.Description | |
| } | |
| return json.Marshal(m) | |
| } | |
| // UserAttributeRead is a user schema attribute as returned by the V2 API. | |
| // It is an alias of the canonical ResourceAttributeRead model to avoid | |
| // duplication and drift in field definitions and helper methods. | |
| type UserAttributeRead = ResourceAttributeRead | |
| func NewUserAttributeReadWithDefaults() *UserAttributeRead { | |
| return (*UserAttributeRead)(NewResourceAttributeReadWithDefaults()) | |
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Custom MarshalJSON without matching UnmarshalJSON — potential data loss on round-trip
UserAttributeRead defines a custom MarshalJSON but relies on the default struct-tag-based UnmarshalJSON. While this works for basic deserialization from the API, it creates an asymmetry: the custom MarshalJSON serializes Description conditionally (only when non-nil), but if someone marshals and then unmarshals a UserAttributeRead with a nil Description, the behavior is correct only by coincidence of omitempty tag alignment.
More concretely, ResourceAttributeRead (the analogous type) does not define a custom MarshalJSON at all — it uses the generated code pattern with toSerialize map. The new UserAttributeRead introduces a divergent serialization style.
Suggestion: Either remove the custom MarshalJSON and rely on struct tags (since the json tags already handle the serialization correctly with omitempty), or document why a custom implementation is needed. The struct tags as defined are sufficient for correct JSON output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same
errors.HttpErrorHandlegap applies to all five*ForResourcemethods in this file (lines 50, 72, 106, 126, 146). Each discards the*http.Responsewith_instead of passing it througherrors.HttpErrorHandle(err, httpRes)like every other API method in the codebase.See the comment on line 55 for the full explanation and suggested fix.