api_contract_revision.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package models
  2. import (
  3. "database/sql/driver"
  4. "encoding/json"
  5. "github.com/google/uuid"
  6. "gorm.io/gorm"
  7. )
  8. // APIContractRevision represents a revision of an API contract
  9. type APIContractRevision struct {
  10. gorm.Model
  11. // ID is a UUID for the APIContract
  12. ID uuid.UUID `gorm:"type:uuid;primaryKey" json:"id"`
  13. // Base64Contract is the APIContract as json encoded in base64
  14. Base64Contract string `json:"base64_contract"`
  15. // ClusterID is the ID of the cluster that the config created.
  16. // This should be a foreign key, but GORM doesnt play well with FKs.
  17. ClusterID int `json:"cluster_id"`
  18. // ProjectID is the ID of the project that the config belongs to.
  19. // This should be a foreign key, but GORM doesnt play well with FKs.
  20. ProjectID int `json:"project_id"`
  21. // Condition is the status of the apply that happened for this revision.
  22. // Condition will contain any failure reasons for a revision, or "SUCCESS" if the revision was applied successfully.
  23. // Further details to expand upon this condition are available in ConditionMetadata
  24. Condition string `json:"condition"`
  25. // ConditionMetadata contains all information about the condition of a given revision.
  26. // If condition is "SUCCESS", this will likely be empty.
  27. // This will follow the error response contract, with the following keys:
  28. // {
  29. // "errors": [
  30. // {
  31. // "code": "string",
  32. // "message": "string",
  33. // "metadata": {}
  34. // }
  35. // ]
  36. // }
  37. ConditionMetadata JSONB `json:"condition_metadata" sql:"type:jsonb" gorm:"type:jsonb"`
  38. }
  39. // TableName overrides the table name
  40. func (APIContractRevision) TableName() string {
  41. return "api_contract_revisions"
  42. }
  43. // JSONB implements the jsonb type in postgres for gorm
  44. type JSONB map[string]any
  45. // Value implements the driver.Valuer interface
  46. func (j JSONB) Value() (driver.Value, error) {
  47. valueString, err := json.Marshal(j)
  48. return string(valueString), err
  49. }
  50. // Scan implements the sql.Scanner interface
  51. func (j *JSONB) Scan(value interface{}) error {
  52. if err := json.Unmarshal(value.([]byte), &j); err != nil {
  53. return err
  54. }
  55. return nil
  56. }