From 66a2e256452fcd231cb9cc87c7c9045f5a86471a Mon Sep 17 00:00:00 2001 From: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> Date: Thu, 15 Feb 2024 14:07:35 +0000 Subject: [PATCH] Add more explicite error Signed-off-by: Pierre-Emmanuel Jacquier <15922119+pierre-emmanuelJ@users.noreply.github.com> --- v3/generator/operations/operations.go | 22 +++---- v3/operations.go | 84 +++++++++++++-------------- 2 files changed, 53 insertions(+), 53 deletions(-) diff --git a/v3/generator/operations/operations.go b/v3/generator/operations/operations.go index 933cdf55d..254473895 100644 --- a/v3/generator/operations/operations.go +++ b/v3/generator/operations/operations.go @@ -113,7 +113,7 @@ func renderResponseSchema(name string, op *v3.Operation) ([]byte, error) { return nil } - gettable, err := renderGettable(name, media.Schema) + gettable, err := renderFindable(name, media.Schema) if err != nil { return err } @@ -266,29 +266,29 @@ func renderRequestParametersSchema(name string, op *v3.Operation) ([]byte, error return output.Bytes(), nil } -const gettableTemplate = ` -// Get{{ .TypeName }} attempts to find an {{ .TypeName }} by name or ID. -func (l {{ .ListTypeName }}) Get{{ .TypeName }}(nameOrID string) ({{ .TypeName }}, error) { +const findableTemplate = ` +// Find{{ .TypeName }} attempts to find an {{ .TypeName }} by name or ID. +func (l {{ .ListTypeName }}) Find{{ .TypeName }}(nameOrID string) ({{ .TypeName }}, error) { for i, elem := range l.{{ .ListFieldName }} { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.{{ .ListFieldName }}[i], nil } } - return {{ .TypeName }}{}, ErrNotFound + return {{ .TypeName }}{}, fmt.Errorf("%q not found in {{ .ListTypeName }}: %w", nameOrID, ErrNotFound) } ` -type Gettable struct { +type Findable struct { TypeName string ListTypeName string ListFieldName string } -// renderGettable renders a gettable method on listable resource. -// this gettable method get the resource by name or id if available. +// renderFindable renders a find method on listable resource. +// this find method get the resource by name or id if available. // returns nil on non listable resources. -func renderGettable(funcName string, s *base.SchemaProxy) ([]byte, error) { +func renderFindable(funcName string, s *base.SchemaProxy) ([]byte, error) { sc, err := s.BuildSchema() if err != nil { return nil, err @@ -336,11 +336,11 @@ func renderGettable(funcName string, s *base.SchemaProxy) ([]byte, error) { _, hasID := item.Properties["id"] if hasName && hasID { output := bytes.NewBuffer([]byte{}) - t, err := template.New("Gettable").Parse(gettableTemplate) + t, err := template.New("Findable").Parse(findableTemplate) if err != nil { return nil, err } - if err := t.Execute(output, Gettable{ + if err := t.Execute(output, Findable{ ListTypeName: funcName + "Response", ListFieldName: helpers.ToCamel(propName), TypeName: typeName, diff --git a/v3/operations.go b/v3/operations.go index 720051e02..e4141d33b 100755 --- a/v3/operations.go +++ b/v3/operations.go @@ -299,15 +299,15 @@ type ListAntiAffinityGroupsResponse struct { AntiAffinityGroups []AntiAffinityGroup `json:"anti-affinity-groups,omitempty"` } -// GetAntiAffinityGroup attempts to find an AntiAffinityGroup by name or ID. -func (l ListAntiAffinityGroupsResponse) GetAntiAffinityGroup(nameOrID string) (AntiAffinityGroup, error) { +// FindAntiAffinityGroup attempts to find an AntiAffinityGroup by name or ID. +func (l ListAntiAffinityGroupsResponse) FindAntiAffinityGroup(nameOrID string) (AntiAffinityGroup, error) { for i, elem := range l.AntiAffinityGroups { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.AntiAffinityGroups[i], nil } } - return AntiAffinityGroup{}, ErrNotFound + return AntiAffinityGroup{}, fmt.Errorf("%q not found in ListAntiAffinityGroupsResponse: %w", nameOrID, ErrNotFound) } // List Anti-affinity Groups @@ -682,15 +682,15 @@ type ListBlockStorageVolumesResponse struct { BlockStorageVolumes []BlockStorageVolume `json:"block-storage-volumes,omitempty"` } -// GetBlockStorageVolume attempts to find an BlockStorageVolume by name or ID. -func (l ListBlockStorageVolumesResponse) GetBlockStorageVolume(nameOrID string) (BlockStorageVolume, error) { +// FindBlockStorageVolume attempts to find an BlockStorageVolume by name or ID. +func (l ListBlockStorageVolumesResponse) FindBlockStorageVolume(nameOrID string) (BlockStorageVolume, error) { for i, elem := range l.BlockStorageVolumes { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.BlockStorageVolumes[i], nil } } - return BlockStorageVolume{}, ErrNotFound + return BlockStorageVolume{}, fmt.Errorf("%q not found in ListBlockStorageVolumesResponse: %w", nameOrID, ErrNotFound) } type ListBlockStorageVolumesOpt func(url.Values) @@ -815,15 +815,15 @@ type ListBlockStorageSnapshotsResponse struct { BlockStorageSnapshots []BlockStorageSnapshot `json:"block-storage-snapshots,omitempty"` } -// GetBlockStorageSnapshot attempts to find an BlockStorageSnapshot by name or ID. -func (l ListBlockStorageSnapshotsResponse) GetBlockStorageSnapshot(nameOrID string) (BlockStorageSnapshot, error) { +// FindBlockStorageSnapshot attempts to find an BlockStorageSnapshot by name or ID. +func (l ListBlockStorageSnapshotsResponse) FindBlockStorageSnapshot(nameOrID string) (BlockStorageSnapshot, error) { for i, elem := range l.BlockStorageSnapshots { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.BlockStorageSnapshots[i], nil } } - return BlockStorageSnapshot{}, ErrNotFound + return BlockStorageSnapshot{}, fmt.Errorf("%q not found in ListBlockStorageSnapshotsResponse: %w", nameOrID, ErrNotFound) } // List block storage snapshots @@ -6129,15 +6129,15 @@ type ListDeployTargetsResponse struct { DeployTargets []DeployTarget `json:"deploy-targets,omitempty"` } -// GetDeployTarget attempts to find an DeployTarget by name or ID. -func (l ListDeployTargetsResponse) GetDeployTarget(nameOrID string) (DeployTarget, error) { +// FindDeployTarget attempts to find an DeployTarget by name or ID. +func (l ListDeployTargetsResponse) FindDeployTarget(nameOrID string) (DeployTarget, error) { for i, elem := range l.DeployTargets { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.DeployTargets[i], nil } } - return DeployTarget{}, ErrNotFound + return DeployTarget{}, fmt.Errorf("%q not found in ListDeployTargetsResponse: %w", nameOrID, ErrNotFound) } // List Deploy Targets @@ -6329,15 +6329,15 @@ type ListDNSDomainRecordsResponse struct { DNSDomainRecords []DNSDomainRecord `json:"dns-domain-records,omitempty"` } -// GetDNSDomainRecord attempts to find an DNSDomainRecord by name or ID. -func (l ListDNSDomainRecordsResponse) GetDNSDomainRecord(nameOrID string) (DNSDomainRecord, error) { +// FindDNSDomainRecord attempts to find an DNSDomainRecord by name or ID. +func (l ListDNSDomainRecordsResponse) FindDNSDomainRecord(nameOrID string) (DNSDomainRecord, error) { for i, elem := range l.DNSDomainRecords { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.DNSDomainRecords[i], nil } } - return DNSDomainRecord{}, ErrNotFound + return DNSDomainRecord{}, fmt.Errorf("%q not found in ListDNSDomainRecordsResponse: %w", nameOrID, ErrNotFound) } // List DNS domain records @@ -7308,15 +7308,15 @@ type ListIAMRolesResponse struct { IAMRoles []IAMRole `json:"iam-roles,omitempty"` } -// GetIAMRole attempts to find an IAMRole by name or ID. -func (l ListIAMRolesResponse) GetIAMRole(nameOrID string) (IAMRole, error) { +// FindIAMRole attempts to find an IAMRole by name or ID. +func (l ListIAMRolesResponse) FindIAMRole(nameOrID string) (IAMRole, error) { for i, elem := range l.IAMRoles { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.IAMRoles[i], nil } } - return IAMRole{}, ErrNotFound + return IAMRole{}, fmt.Errorf("%q not found in ListIAMRolesResponse: %w", nameOrID, ErrNotFound) } // List IAM Roles @@ -7649,15 +7649,15 @@ type ListInstancesResponse struct { Instances []ListInstancesResponseInstances `json:"instances,omitempty"` } -// GetListInstancesResponseInstances attempts to find an ListInstancesResponseInstances by name or ID. -func (l ListInstancesResponse) GetListInstancesResponseInstances(nameOrID string) (ListInstancesResponseInstances, error) { +// FindListInstancesResponseInstances attempts to find an ListInstancesResponseInstances by name or ID. +func (l ListInstancesResponse) FindListInstancesResponseInstances(nameOrID string) (ListInstancesResponseInstances, error) { for i, elem := range l.Instances { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.Instances[i], nil } } - return ListInstancesResponseInstances{}, ErrNotFound + return ListInstancesResponseInstances{}, fmt.Errorf("%q not found in ListInstancesResponse: %w", nameOrID, ErrNotFound) } type ListInstancesManagerType string @@ -7818,15 +7818,15 @@ type ListInstancePoolsResponse struct { InstancePools []InstancePool `json:"instance-pools,omitempty"` } -// GetInstancePool attempts to find an InstancePool by name or ID. -func (l ListInstancePoolsResponse) GetInstancePool(nameOrID string) (InstancePool, error) { +// FindInstancePool attempts to find an InstancePool by name or ID. +func (l ListInstancePoolsResponse) FindInstancePool(nameOrID string) (InstancePool, error) { for i, elem := range l.InstancePools { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.InstancePools[i], nil } } - return InstancePool{}, ErrNotFound + return InstancePool{}, fmt.Errorf("%q not found in ListInstancePoolsResponse: %w", nameOrID, ErrNotFound) } // List Instance Pools @@ -9159,15 +9159,15 @@ type ListLoadBalancersResponse struct { LoadBalancers []LoadBalancer `json:"load-balancers,omitempty"` } -// GetLoadBalancer attempts to find an LoadBalancer by name or ID. -func (l ListLoadBalancersResponse) GetLoadBalancer(nameOrID string) (LoadBalancer, error) { +// FindLoadBalancer attempts to find an LoadBalancer by name or ID. +func (l ListLoadBalancersResponse) FindLoadBalancer(nameOrID string) (LoadBalancer, error) { for i, elem := range l.LoadBalancers { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.LoadBalancers[i], nil } } - return LoadBalancer{}, ErrNotFound + return LoadBalancer{}, fmt.Errorf("%q not found in ListLoadBalancersResponse: %w", nameOrID, ErrNotFound) } // List Load Balancers @@ -9799,15 +9799,15 @@ type ListPrivateNetworksResponse struct { PrivateNetworks []PrivateNetwork `json:"private-networks,omitempty"` } -// GetPrivateNetwork attempts to find an PrivateNetwork by name or ID. -func (l ListPrivateNetworksResponse) GetPrivateNetwork(nameOrID string) (PrivateNetwork, error) { +// FindPrivateNetwork attempts to find an PrivateNetwork by name or ID. +func (l ListPrivateNetworksResponse) FindPrivateNetwork(nameOrID string) (PrivateNetwork, error) { for i, elem := range l.PrivateNetworks { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.PrivateNetworks[i], nil } } - return PrivateNetwork{}, ErrNotFound + return PrivateNetwork{}, fmt.Errorf("%q not found in ListPrivateNetworksResponse: %w", nameOrID, ErrNotFound) } // List Private Networks @@ -10652,15 +10652,15 @@ type ListSecurityGroupsResponse struct { SecurityGroups []SecurityGroup `json:"security-groups,omitempty"` } -// GetSecurityGroup attempts to find an SecurityGroup by name or ID. -func (l ListSecurityGroupsResponse) GetSecurityGroup(nameOrID string) (SecurityGroup, error) { +// FindSecurityGroup attempts to find an SecurityGroup by name or ID. +func (l ListSecurityGroupsResponse) FindSecurityGroup(nameOrID string) (SecurityGroup, error) { for i, elem := range l.SecurityGroups { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.SecurityGroups[i], nil } } - return SecurityGroup{}, ErrNotFound + return SecurityGroup{}, fmt.Errorf("%q not found in ListSecurityGroupsResponse: %w", nameOrID, ErrNotFound) } type ListSecurityGroupsVisibility string @@ -11227,15 +11227,15 @@ type ListSKSClustersResponse struct { SKSClusters []SKSCluster `json:"sks-clusters,omitempty"` } -// GetSKSCluster attempts to find an SKSCluster by name or ID. -func (l ListSKSClustersResponse) GetSKSCluster(nameOrID string) (SKSCluster, error) { +// FindSKSCluster attempts to find an SKSCluster by name or ID. +func (l ListSKSClustersResponse) FindSKSCluster(nameOrID string) (SKSCluster, error) { for i, elem := range l.SKSClusters { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.SKSClusters[i], nil } } - return SKSCluster{}, ErrNotFound + return SKSCluster{}, fmt.Errorf("%q not found in ListSKSClustersResponse: %w", nameOrID, ErrNotFound) } // List SKS clusters @@ -12392,15 +12392,15 @@ type ListSnapshotsResponse struct { Snapshots []Snapshot `json:"snapshots,omitempty"` } -// GetSnapshot attempts to find an Snapshot by name or ID. -func (l ListSnapshotsResponse) GetSnapshot(nameOrID string) (Snapshot, error) { +// FindSnapshot attempts to find an Snapshot by name or ID. +func (l ListSnapshotsResponse) FindSnapshot(nameOrID string) (Snapshot, error) { for i, elem := range l.Snapshots { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.Snapshots[i], nil } } - return Snapshot{}, ErrNotFound + return Snapshot{}, fmt.Errorf("%q not found in ListSnapshotsResponse: %w", nameOrID, ErrNotFound) } // List Snapshots @@ -12931,15 +12931,15 @@ type ListTemplatesResponse struct { Templates []Template `json:"templates,omitempty"` } -// GetTemplate attempts to find an Template by name or ID. -func (l ListTemplatesResponse) GetTemplate(nameOrID string) (Template, error) { +// FindTemplate attempts to find an Template by name or ID. +func (l ListTemplatesResponse) FindTemplate(nameOrID string) (Template, error) { for i, elem := range l.Templates { if elem.Name == nameOrID || elem.ID.String() == nameOrID { return l.Templates[i], nil } } - return Template{}, ErrNotFound + return Template{}, fmt.Errorf("%q not found in ListTemplatesResponse: %w", nameOrID, ErrNotFound) } type ListTemplatesVisibility string