Skip to content

Commit

Permalink
chore: additional linters to improve code quality
Browse files Browse the repository at this point in the history
linters:
    - ireturn
    - importas
- repository interface improvement
  • Loading branch information
Muchogoc committed Mar 8, 2023
1 parent 3cf85d9 commit d647cc9
Show file tree
Hide file tree
Showing 18 changed files with 396 additions and 315 deletions.
2 changes: 2 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ linters:
- contextcheck
- gocritic
- nilerr
- ireturn
- importas

linters-settings:
staticcheck:
Expand Down
4 changes: 2 additions & 2 deletions pkg/clinical/application/extensions/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type ISCClientExtension interface {
type ISCExtensionImpl struct{}

// NewISCExtension initializes an ISC extension
func NewISCExtension() ISCClientExtension {
func NewISCExtension() *ISCExtensionImpl {
return &ISCExtensionImpl{}
}

Expand Down Expand Up @@ -59,7 +59,7 @@ type BaseExtensionImpl struct {
}

// NewBaseExtensionImpl ...
func NewBaseExtensionImpl() BaseExtension {
func NewBaseExtensionImpl() *BaseExtensionImpl {
return &BaseExtensionImpl{}
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/clinical/domain/patient.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,8 +312,8 @@ type PatientLinkEdge struct {
func (pl *PatientLink) IsNode() {}

// GetID returns the patient links primary key
func (pl *PatientLink) GetID() firebasetools.ID {
return firebasetools.IDValue(pl.ID)
func (pl *PatientLink) GetID() string {
return pl.ID
}

// SetID sets the patient links' id
Expand Down
39 changes: 14 additions & 25 deletions pkg/clinical/infrastructure/datastore/cloudhealthcare/fhir.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"time"

Expand All @@ -14,7 +13,6 @@ import (
"github.com/savannahghi/clinical/pkg/clinical/application/common/helpers"
"github.com/savannahghi/clinical/pkg/clinical/domain"
dataset "github.com/savannahghi/clinical/pkg/clinical/infrastructure/datastore/cloudhealthcare/fhirdataset"
"github.com/savannahghi/clinical/pkg/clinical/repository"
"github.com/savannahghi/converterandformatter"
"github.com/savannahghi/firebasetools"
"github.com/savannahghi/scalarutils"
Expand All @@ -28,6 +26,7 @@ const (

// resource types
const (
organizationResource = "Organization"
patientResourceType = "Patient"
episodeOfCareResourceType = "EpisodeOfCare"
observationResourceType = "Observation"
Expand All @@ -38,6 +37,7 @@ const (
encounterResourceType = "Encounter"
compositionResourceType = "Composition"
medicationStatementResourceType = "MedicationStatement"
medicationResourceType = "Medication"
)

// StoreImpl represents the FHIR infrastructure implementation
Expand All @@ -48,7 +48,7 @@ type StoreImpl struct {
// NewFHIRStoreImpl initializes the new FHIR implementation
func NewFHIRStoreImpl(
dataset dataset.FHIRRepository,
) repository.FHIR {
) *StoreImpl {
return &StoreImpl{
Dataset: dataset,
}
Expand Down Expand Up @@ -234,23 +234,22 @@ func (fh StoreImpl) CreateFHIRCondition(ctx context.Context, input domain.FHIRCo

// CreateFHIROrganization creates a FHIROrganization instance
func (fh StoreImpl) CreateFHIROrganization(ctx context.Context, input domain.FHIROrganizationInput) (*domain.FHIROrganizationRelayPayload, error) {
resourceType := "Organization"
resource := domain.FHIROrganization{}

payload, err := converterandformatter.StructToMap(input)
if err != nil {
return nil, fmt.Errorf("unable to turn %s input into a map: %w", resourceType, err)
return nil, fmt.Errorf("unable to turn %s input into a map: %w", organizationResource, err)
}

data, err := fh.Dataset.CreateFHIRResource(resourceType, payload)
data, err := fh.Dataset.CreateFHIRResource(organizationResource, payload)
if err != nil {
return nil, fmt.Errorf("unable to create %s resource: %w", resourceType, err)
return nil, fmt.Errorf("unable to create %s resource: %w", organizationResource, err)
}
err = json.Unmarshal(data, &resource)
if err != nil {
return nil, fmt.Errorf(
"unable to unmarshal %s response JSON: data: %v\n, error: %w",
resourceType, string(data), err)
organizationResource, string(data), err)
}

output := &domain.FHIROrganizationRelayPayload{
Expand All @@ -261,10 +260,8 @@ func (fh StoreImpl) CreateFHIROrganization(ctx context.Context, input domain.FHI

// SearchFHIROrganization provides a search API for FHIROrganization
func (fh StoreImpl) SearchFHIROrganization(ctx context.Context, params map[string]interface{}) (*domain.FHIROrganizationRelayConnection, error) {

resourceName := "Organization"
output := domain.FHIROrganizationRelayConnection{}
resources, err := fh.searchFilterHelper(ctx, resourceName, params)
resources, err := fh.searchFilterHelper(ctx, organizationResource, params)
if err != nil {
return nil, err
}
Expand All @@ -280,7 +277,7 @@ func (fh StoreImpl) SearchFHIROrganization(ctx context.Context, params map[strin
err = json.Unmarshal(resourceBs, &resource)
if err != nil {
return nil, fmt.Errorf(
"server error: Unable to unmarshal %s: %w", resourceName, err)
"server error: Unable to unmarshal %s: %w", organizationResource, err)
}
output.Edges = append(output.Edges, &domain.FHIROrganizationRelayEdge{
Node: &resource,
Expand All @@ -295,7 +292,7 @@ func (fh StoreImpl) FindOrganizationByID(ctx context.Context, organizationID str
return nil, fmt.Errorf("organization ID is required")
}

data, err := fh.Dataset.GetFHIRResource("Organization", organizationID)
data, err := fh.Dataset.GetFHIRResource(organizationResource, organizationID)
if err != nil {
return nil, fmt.Errorf("unable to retrieve organization: %w", err)
}
Expand Down Expand Up @@ -1447,25 +1444,23 @@ func (fh *StoreImpl) CreateFHIRMedicationStatement(ctx context.Context, input do

// CreateFHIRMedication creates a new FHIR Medication instance
func (fh *StoreImpl) CreateFHIRMedication(ctx context.Context, input domain.FHIRMedicationInput) (*domain.FHIRMedicationRelayPayload, error) {
resourceType := "Medication"

resource := domain.FHIRMedication{}

payload, err := converterandformatter.StructToMap(input)
if err != nil {
return nil, fmt.Errorf("unable to turn %s input into a map: %w", resourceType, err)
return nil, fmt.Errorf("unable to turn %s input into a map: %w", medicationResourceType, err)
}

data, err := fh.Dataset.CreateFHIRResource(resourceType, payload)
data, err := fh.Dataset.CreateFHIRResource(medicationResourceType, payload)
if err != nil {
return nil, fmt.Errorf("unable to create/update %s resource: %w", resourceType, err)
return nil, fmt.Errorf("unable to create/update %s resource: %w", medicationResourceType, err)
}

err = json.Unmarshal(data, &resource)
if err != nil {
return nil, fmt.Errorf(
"unable to unmarshal %s response JSON: data: %v\n, error: %w",
resourceType, string(data), err)
medicationResourceType, string(data), err)
}

output := &domain.FHIRMedicationRelayPayload{
Expand Down Expand Up @@ -1668,9 +1663,3 @@ func (fh *StoreImpl) POSTRequest(resourceName string, path string, params url.Va
func (fh *StoreImpl) GetFHIRResource(resourceType, fhirResourceID string) ([]byte, error) {
return fh.Dataset.GetFHIRResource(resourceType, fhirResourceID)
}

// FHIRHeaders composes suitable FHIR headers, with authentication and content
// type already set
func (fh *StoreImpl) FHIRHeaders() (http.Header, error) {
return fh.Dataset.FHIRHeaders()
}
Loading

0 comments on commit d647cc9

Please sign in to comment.