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

     1  package model
     2  
     3  import (
     4  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
     5  	"github.com/pkg/errors"
     6  )
     7  
     8  // Spec represents a specification of a resource.
     9  type Spec struct {
    10  	ID         string
    11  	ObjectType SpecReferenceObjectType
    12  	ObjectID   string
    13  
    14  	Data       *string
    15  	Format     SpecFormat
    16  	APIType    *APISpecType
    17  	EventType  *EventSpecType
    18  	CustomType *string
    19  }
    20  
    21  // SpecReferenceObjectType represents the type of the referenced object by the Spec.
    22  type SpecReferenceObjectType string
    23  
    24  const (
    25  	// APISpecReference is a reference to an API Specification.
    26  	APISpecReference SpecReferenceObjectType = "API"
    27  	// EventSpecReference is a reference to an Event Specification.
    28  	EventSpecReference SpecReferenceObjectType = "Event"
    29  )
    30  
    31  // GetResourceType returns the resource type of the specification based on the referenced entity.
    32  func (obj SpecReferenceObjectType) GetResourceType() resource.Type {
    33  	switch obj {
    34  	case APISpecReference:
    35  		return resource.APISpecification
    36  	case EventSpecReference:
    37  		return resource.EventSpecification
    38  	}
    39  	return ""
    40  }
    41  
    42  // SpecFormat is the format of the specification.
    43  type SpecFormat string
    44  
    45  const (
    46  	// SpecFormatYaml is the YAML format.
    47  	SpecFormatYaml SpecFormat = "YAML"
    48  	// SpecFormatJSON is the JSON format.
    49  	SpecFormatJSON SpecFormat = "JSON"
    50  	// SpecFormatXML is the XML format.
    51  	SpecFormatXML SpecFormat = "XML"
    52  
    53  	// ORD Formats
    54  
    55  	// SpecFormatApplicationJSON is the Application JSON format.
    56  	SpecFormatApplicationJSON SpecFormat = "application/json"
    57  	// SpecFormatTextYAML is the Text YAML format.
    58  	SpecFormatTextYAML SpecFormat = "text/yaml"
    59  	// SpecFormatApplicationXML is the Application XML format.
    60  	SpecFormatApplicationXML SpecFormat = "application/xml"
    61  	// SpecFormatPlainText is the Plain Text format.
    62  	SpecFormatPlainText SpecFormat = "text/plain"
    63  	// SpecFormatOctetStream is the Octet Stream format.
    64  	SpecFormatOctetStream SpecFormat = "application/octet-stream"
    65  )
    66  
    67  // APISpecType is the type of the API Specification.
    68  type APISpecType string
    69  
    70  const (
    71  	// APISpecTypeOdata is the OData Specification.
    72  	APISpecTypeOdata APISpecType = "ODATA"
    73  	// APISpecTypeOpenAPI is the OpenAPI Specification.
    74  	APISpecTypeOpenAPI APISpecType = "OPEN_API"
    75  
    76  	// ORD Formats
    77  
    78  	// APISpecTypeOpenAPIV2 is the OpenAPI V2 Specification.
    79  	APISpecTypeOpenAPIV2 APISpecType = "openapi-v2"
    80  	// APISpecTypeOpenAPIV3 is the OpenAPI V3 Specification.
    81  	APISpecTypeOpenAPIV3 APISpecType = "openapi-v3"
    82  	// APISpecTypeRaml is the RAML Specification.
    83  	APISpecTypeRaml APISpecType = "raml-v1"
    84  	// APISpecTypeEDMX is the EDMX Specification.
    85  	APISpecTypeEDMX APISpecType = "edmx"
    86  	// APISpecTypeCsdl is the CSDL Specification.
    87  	APISpecTypeCsdl APISpecType = "csdl-json"
    88  	// APISpecTypeWsdlV1 is the WSDL V1 Specification.
    89  	APISpecTypeWsdlV1 APISpecType = "wsdl-v1"
    90  	// APISpecTypeWsdlV2 is the WSDL V2 Specification.
    91  	APISpecTypeWsdlV2 APISpecType = "wsdl-v2"
    92  	// APISpecTypeRfcMetadata is the RFC Metadata Specification.
    93  	APISpecTypeRfcMetadata APISpecType = "sap-rfc-metadata-v1"
    94  	// APISpecTypeSQLAPIDefinitionV1 is the SQL API Definition Specification.
    95  	APISpecTypeSQLAPIDefinitionV1 APISpecType = "sap-sql-api-definition-v1"
    96  	// APISpecTypeCustom is the Custom Specification.
    97  	APISpecTypeCustom APISpecType = "custom"
    98  )
    99  
   100  // EventSpecType is the type of the Event Specification.
   101  type EventSpecType string
   102  
   103  const (
   104  	// EventSpecTypeAsyncAPI is the AsyncAPI Specification.
   105  	EventSpecTypeAsyncAPI EventSpecType = "ASYNC_API"
   106  
   107  	// ORD Formats
   108  
   109  	// EventSpecTypeAsyncAPIV2 is the AsyncAPI V2 Specification.
   110  	EventSpecTypeAsyncAPIV2 EventSpecType = "asyncapi-v2"
   111  	// EventSpecTypeCustom is the Custom Specification.
   112  	EventSpecTypeCustom EventSpecType = "custom"
   113  )
   114  
   115  // SpecInput is an input for creating/updating specification.
   116  type SpecInput struct {
   117  	Data       *string
   118  	Format     SpecFormat
   119  	APIType    *APISpecType
   120  	EventType  *EventSpecType
   121  	CustomType *string
   122  
   123  	FetchRequest *FetchRequestInput
   124  }
   125  
   126  // ToSpec converts the input to a Spec.
   127  func (s *SpecInput) ToSpec(id string, objectType SpecReferenceObjectType, objectID string) (*Spec, error) {
   128  	if s == nil {
   129  		return nil, nil
   130  	}
   131  
   132  	if objectType == APISpecReference && s.APIType == nil {
   133  		return nil, errors.New("API Spec type cannot be empty")
   134  	}
   135  
   136  	if objectType == EventSpecReference && s.EventType == nil {
   137  		return nil, errors.New("event spec type cannot be empty")
   138  	}
   139  
   140  	return &Spec{
   141  		ID:         id,
   142  		ObjectType: objectType,
   143  		ObjectID:   objectID,
   144  		Data:       s.Data,
   145  		Format:     s.Format,
   146  		APIType:    s.APIType,
   147  		EventType:  s.EventType,
   148  		CustomType: s.CustomType,
   149  	}, nil
   150  }