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

     1  package labeldef
     2  
     3  import (
     4  	"database/sql"
     5  	"encoding/json"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/model"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  // NewConverter missing godoc
    13  func NewConverter() *converter {
    14  	return &converter{}
    15  }
    16  
    17  type converter struct{}
    18  
    19  // FromGraphQL missing godoc
    20  func (c *converter) FromGraphQL(input graphql.LabelDefinitionInput, tenant string) (model.LabelDefinition, error) {
    21  	schema, err := input.Schema.Unmarshal()
    22  	if err != nil {
    23  		return model.LabelDefinition{}, err
    24  	}
    25  
    26  	return model.LabelDefinition{
    27  		Key:    input.Key,
    28  		Schema: schema,
    29  		Tenant: tenant,
    30  	}, nil
    31  }
    32  
    33  // ToGraphQL missing godoc
    34  func (c *converter) ToGraphQL(in model.LabelDefinition) (graphql.LabelDefinition, error) {
    35  	schema, err := graphql.MarshalSchema(in.Schema)
    36  	if err != nil {
    37  		return graphql.LabelDefinition{}, err
    38  	}
    39  	return graphql.LabelDefinition{
    40  		Key:    in.Key,
    41  		Schema: schema,
    42  	}, nil
    43  }
    44  
    45  // ToGraphQLInput missing godoc
    46  func (c *converter) ToGraphQLInput(in model.LabelDefinition) (graphql.LabelDefinitionInput, error) {
    47  	schema, err := graphql.MarshalSchema(in.Schema)
    48  	if err != nil {
    49  		return graphql.LabelDefinitionInput{}, err
    50  	}
    51  	return graphql.LabelDefinitionInput{
    52  		Key:    in.Key,
    53  		Schema: schema,
    54  	}, nil
    55  }
    56  
    57  // ToEntity missing godoc
    58  func (c *converter) ToEntity(in model.LabelDefinition) (Entity, error) {
    59  	out := Entity{
    60  		ID:       in.ID,
    61  		Key:      in.Key,
    62  		TenantID: in.Tenant,
    63  		Version:  in.Version,
    64  	}
    65  	if in.Schema != nil {
    66  		b, err := json.Marshal(in.Schema)
    67  		if err != nil {
    68  			return Entity{}, errors.Wrap(err, "while marshaling schema to JSON")
    69  		}
    70  		out.SchemaJSON = sql.NullString{String: string(b), Valid: true}
    71  	} else {
    72  		out.SchemaJSON = sql.NullString{Valid: false}
    73  	}
    74  	return out, nil
    75  }
    76  
    77  // FromEntity missing godoc
    78  func (c *converter) FromEntity(in Entity) (model.LabelDefinition, error) {
    79  	out := model.LabelDefinition{
    80  		ID:      in.ID,
    81  		Key:     in.Key,
    82  		Tenant:  in.TenantID,
    83  		Version: in.Version,
    84  	}
    85  	if in.SchemaJSON.Valid {
    86  		mapDest := map[string]interface{}{}
    87  		var tmp interface{}
    88  		err := json.Unmarshal([]byte(in.SchemaJSON.String), &mapDest)
    89  		if err != nil {
    90  			return model.LabelDefinition{}, err
    91  		}
    92  		tmp = mapDest
    93  		out.Schema = &tmp
    94  	}
    95  	return out, nil
    96  }