github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/fetchrequest/converter.go (about)

     1  package fetchrequest
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
     9  
    10  	"github.com/kyma-incubator/compass/components/director/internal/model"
    11  	"github.com/kyma-incubator/compass/components/director/internal/repo"
    12  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    13  	"github.com/pkg/errors"
    14  )
    15  
    16  // AuthConverter missing godoc
    17  //go:generate mockery --name=AuthConverter --output=automock --outpkg=automock --case=underscore --disable-version-string
    18  type AuthConverter interface {
    19  	ToGraphQL(in *model.Auth) (*graphql.Auth, error)
    20  	InputFromGraphQL(in *graphql.AuthInput) (*model.AuthInput, error)
    21  }
    22  
    23  type converter struct {
    24  	authConverter AuthConverter
    25  }
    26  
    27  // NewConverter missing godoc
    28  func NewConverter(authConverter AuthConverter) *converter {
    29  	return &converter{authConverter: authConverter}
    30  }
    31  
    32  // ToGraphQL missing godoc
    33  func (c *converter) ToGraphQL(in *model.FetchRequest) (*graphql.FetchRequest, error) {
    34  	if in == nil {
    35  		return nil, nil
    36  	}
    37  
    38  	auth, err := c.authConverter.ToGraphQL(in.Auth)
    39  	if err != nil {
    40  		return nil, errors.Wrap(err, "while converting Auth to GraphQL")
    41  	}
    42  
    43  	return &graphql.FetchRequest{
    44  		URL:    in.URL,
    45  		Auth:   auth,
    46  		Mode:   graphql.FetchMode(in.Mode),
    47  		Filter: in.Filter,
    48  		Status: c.statusToGraphQL(in.Status),
    49  	}, nil
    50  }
    51  
    52  // InputFromGraphQL missing godoc
    53  func (c *converter) InputFromGraphQL(in *graphql.FetchRequestInput) (*model.FetchRequestInput, error) {
    54  	if in == nil {
    55  		return nil, nil
    56  	}
    57  
    58  	var mode *model.FetchMode
    59  	if in.Mode != nil {
    60  		tmp := model.FetchMode(*in.Mode)
    61  		mode = &tmp
    62  	}
    63  
    64  	auth, err := c.authConverter.InputFromGraphQL(in.Auth)
    65  	if err != nil {
    66  		return nil, errors.Wrap(err, "while converting Auth input from GraphQL")
    67  	}
    68  
    69  	return &model.FetchRequestInput{
    70  		URL:    in.URL,
    71  		Auth:   auth,
    72  		Mode:   mode,
    73  		Filter: in.Filter,
    74  	}, nil
    75  }
    76  
    77  // ToEntity missing godoc
    78  func (c *converter) ToEntity(in *model.FetchRequest) (*Entity, error) {
    79  	if in.Status == nil {
    80  		return nil, apperrors.NewInvalidDataError("Invalid input model")
    81  	}
    82  
    83  	auth, err := c.authToEntity(in.Auth)
    84  	if err != nil {
    85  		return nil, errors.Wrap(err, "while converting Auth")
    86  	}
    87  
    88  	filter := repo.NewNullableString(in.Filter)
    89  	message := repo.NewNullableString(in.Status.Message)
    90  	refID := repo.NewValidNullableString(in.ObjectID)
    91  
    92  	var specID sql.NullString
    93  	var documentID sql.NullString
    94  	switch in.ObjectType {
    95  	case model.APISpecFetchRequestReference:
    96  		fallthrough
    97  	case model.EventSpecFetchRequestReference:
    98  		specID = refID
    99  	case model.DocumentFetchRequestReference:
   100  		documentID = refID
   101  	}
   102  
   103  	return &Entity{
   104  		ID:              in.ID,
   105  		URL:             in.URL,
   106  		Auth:            auth,
   107  		SpecID:          specID,
   108  		DocumentID:      documentID,
   109  		Mode:            string(in.Mode),
   110  		Filter:          filter,
   111  		StatusCondition: string(in.Status.Condition),
   112  		StatusMessage:   message,
   113  		StatusTimestamp: in.Status.Timestamp,
   114  	}, nil
   115  }
   116  
   117  // FromEntity missing godoc
   118  func (c *converter) FromEntity(in *Entity, objectType model.FetchRequestReferenceObjectType) (*model.FetchRequest, error) {
   119  	objectID, err := c.objectIDFromEntity(*in)
   120  	if err != nil {
   121  		return nil, errors.Wrap(err, "while determining object reference")
   122  	}
   123  
   124  	auth, err := c.authToModel(in.Auth)
   125  	if err != nil {
   126  		return nil, errors.Wrap(err, "while converting Auth")
   127  	}
   128  
   129  	return &model.FetchRequest{
   130  		ID:         in.ID,
   131  		ObjectID:   objectID,
   132  		ObjectType: objectType,
   133  		Status: &model.FetchRequestStatus{
   134  			Timestamp: in.StatusTimestamp,
   135  			Message:   repo.StringPtrFromNullableString(in.StatusMessage),
   136  			Condition: model.FetchRequestStatusCondition(in.StatusCondition),
   137  		},
   138  		URL:    in.URL,
   139  		Mode:   model.FetchMode(in.Mode),
   140  		Filter: repo.StringPtrFromNullableString(in.Filter),
   141  		Auth:   auth,
   142  	}, nil
   143  }
   144  
   145  func (c *converter) statusToGraphQL(in *model.FetchRequestStatus) *graphql.FetchRequestStatus {
   146  	if in == nil {
   147  		return &graphql.FetchRequestStatus{
   148  			Condition: graphql.FetchRequestStatusConditionInitial,
   149  		}
   150  	}
   151  
   152  	var condition graphql.FetchRequestStatusCondition
   153  	switch in.Condition {
   154  	case model.FetchRequestStatusConditionInitial:
   155  		condition = graphql.FetchRequestStatusConditionInitial
   156  	case model.FetchRequestStatusConditionFailed:
   157  		condition = graphql.FetchRequestStatusConditionFailed
   158  	case model.FetchRequestStatusConditionSucceeded:
   159  		condition = graphql.FetchRequestStatusConditionSucceeded
   160  	default:
   161  		condition = graphql.FetchRequestStatusConditionInitial
   162  	}
   163  
   164  	return &graphql.FetchRequestStatus{
   165  		Condition: condition,
   166  		Message:   in.Message,
   167  		Timestamp: graphql.Timestamp(in.Timestamp),
   168  	}
   169  }
   170  
   171  func (c *converter) authToEntity(in *model.Auth) (sql.NullString, error) {
   172  	var auth sql.NullString
   173  	if in == nil {
   174  		return sql.NullString{}, nil
   175  	}
   176  
   177  	authMarshalled, err := json.Marshal(in)
   178  	if err != nil {
   179  		return sql.NullString{}, errors.Wrap(err, "while marshalling Auth")
   180  	}
   181  
   182  	auth = repo.NewValidNullableString(string(authMarshalled))
   183  	return auth, nil
   184  }
   185  
   186  func (c *converter) authToModel(in sql.NullString) (*model.Auth, error) {
   187  	if !in.Valid {
   188  		return nil, nil
   189  	}
   190  
   191  	var auth model.Auth
   192  	err := json.Unmarshal([]byte(in.String), &auth)
   193  	if err != nil {
   194  		return nil, errors.Wrap(err, "while unmarshalling Auth")
   195  	}
   196  
   197  	return &auth, nil
   198  }
   199  
   200  func (c *converter) objectIDFromEntity(in Entity) (string, error) {
   201  	if in.SpecID.Valid {
   202  		return in.SpecID.String, nil
   203  	}
   204  
   205  	if in.DocumentID.Valid {
   206  		return in.DocumentID.String, nil
   207  	}
   208  
   209  	return "", fmt.Errorf("incorrect Object Reference ID and its type for Entity with ID %q", in.ID)
   210  }