From 8b25459ccf240e2b5eb9fb6be867f3cb8f33a90b Mon Sep 17 00:00:00 2001 From: Sourav Bhowmik Date: Mon, 18 Nov 2024 13:27:25 +0100 Subject: [PATCH] feat: added new category field in obligations --- cmd/laas/docs/docs.go | 21 ++++++++++++++++++ cmd/laas/docs/swagger.json | 21 ++++++++++++++++++ cmd/laas/docs/swagger.yaml | 17 ++++++++++++++ pkg/models/types.go | 45 +++++++++++++++++++++++++++++++++++++- 4 files changed, 103 insertions(+), 1 deletion(-) diff --git a/cmd/laas/docs/docs.go b/cmd/laas/docs/docs.go index f8db8af..f14c3eb 100644 --- a/cmd/laas/docs/docs.go +++ b/cmd/laas/docs/docs.go @@ -2647,6 +2647,18 @@ const docTemplate = `{ "active": { "type": "boolean" }, + "category": { + "type": "string", + "enum": [ + "DISTRIBUTION", + "PATENT", + "INTERNAL", + "CONTRACTUAL", + "EXPORT_CONTROL", + "GENERAL" + ], + "example": "DISTRIBUTION" + }, "classification": { "$ref": "#/definitions/models.ObligationClassification" }, @@ -2726,6 +2738,7 @@ const docTemplate = `{ "models.ObligationDTO": { "type": "object", "required": [ + "category", "classification", "shortnames", "text", @@ -2736,6 +2749,10 @@ const docTemplate = `{ "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" @@ -2919,6 +2936,10 @@ const docTemplate = `{ "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" diff --git a/cmd/laas/docs/swagger.json b/cmd/laas/docs/swagger.json index 70bd241..eb78760 100644 --- a/cmd/laas/docs/swagger.json +++ b/cmd/laas/docs/swagger.json @@ -2640,6 +2640,18 @@ "active": { "type": "boolean" }, + "category": { + "type": "string", + "enum": [ + "DISTRIBUTION", + "PATENT", + "INTERNAL", + "CONTRACTUAL", + "EXPORT_CONTROL", + "GENERAL" + ], + "example": "DISTRIBUTION" + }, "classification": { "$ref": "#/definitions/models.ObligationClassification" }, @@ -2719,6 +2731,7 @@ "models.ObligationDTO": { "type": "object", "required": [ + "category", "classification", "shortnames", "text", @@ -2729,6 +2742,10 @@ "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" @@ -2912,6 +2929,10 @@ "active": { "type": "boolean" }, + "category": { + "type": "string", + "example": "DISTRIBUTION" + }, "classification": { "type": "string", "example": "GREEN" diff --git a/cmd/laas/docs/swagger.yaml b/cmd/laas/docs/swagger.yaml index 08e961a..67655f0 100644 --- a/cmd/laas/docs/swagger.yaml +++ b/cmd/laas/docs/swagger.yaml @@ -332,6 +332,16 @@ definitions: properties: active: type: boolean + category: + enum: + - DISTRIBUTION + - PATENT + - INTERNAL + - CONTRACTUAL + - EXPORT_CONTROL + - GENERAL + example: DISTRIBUTION + type: string classification: $ref: '#/definitions/models.ObligationClassification' comment: @@ -387,6 +397,9 @@ definitions: properties: active: type: boolean + category: + example: DISTRIBUTION + type: string classification: example: GREEN type: string @@ -415,6 +428,7 @@ definitions: example: RISK type: string required: + - category - classification - shortnames - text @@ -520,6 +534,9 @@ definitions: properties: active: type: boolean + category: + example: DISTRIBUTION + type: string classification: example: GREEN type: string diff --git a/pkg/models/types.go b/pkg/models/types.go index a39a621..8ff55ef 100644 --- a/pkg/models/types.go +++ b/pkg/models/types.go @@ -13,6 +13,7 @@ import ( "encoding/json" "errors" "fmt" + "strings" "time" "github.com/go-playground/validator/v10" @@ -385,6 +386,25 @@ type Obligation struct { Licenses []*LicenseDB `gorm:"many2many:obligation_licenses;"` Type *ObligationType `gorm:"foreignKey:ObligationTypeId"` Classification *ObligationClassification `gorm:"foreignKey:ObligationClassificationId"` + Category *string `json:"category" gorm:"default:GENERAL" enums:"DISTRIBUTION,PATENT,INTERNAL,CONTRACTUAL,EXPORT_CONTROL,GENERAL" example:"DISTRIBUTION"` +} + +var validCategories = []string{"DISTRIBUTION", "PATENT", "INTERNAL", "CONTRACTUAL", "EXPORT_CONTROL", "GENERAL"} + +func validateCategory(o *Obligation) error { + allCategories := strings.Join(validCategories, ", ") + // Check if the provided category is in the list of valid categories + categoryValid := false + for _, cat := range validCategories { + if *o.Category == cat { + categoryValid = true + break + } + } + if !categoryValid { + return fmt.Errorf("category must be one of the following values: %s", allCategories) + } + return nil } func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) { @@ -442,6 +462,10 @@ func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) { } } + if err := validateCategory(o); err != nil { + return err + } + for i := 0; i < len(o.Licenses); i++ { var license LicenseDB if err := tx.Where(LicenseDB{Shortname: o.Licenses[i].Shortname}).First(&license).Error; err != nil { @@ -450,7 +474,7 @@ func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) { o.Licenses[i] = &license } - return + return nil } type ContextKey string @@ -515,6 +539,13 @@ func (o *Obligation) BeforeUpdate(tx *gorm.DB) (err error) { return fmt.Errorf("obligation classification must be one of the following values:%s", allClassifications) } } + + if o.Category != nil { + if err := validateCategory(o); err != nil { + return err + } + } + return } @@ -528,6 +559,7 @@ func (o *Obligation) MarshalJSON() ([]byte, error) { Active: o.Active, TextUpdatable: o.TextUpdatable, Shortnames: []string{}, + Category: o.Category, } if o.Type != nil { @@ -538,6 +570,13 @@ func (o *Obligation) MarshalJSON() ([]byte, error) { ob.Classification = &o.Classification.Classification } + if o.Category != nil && *o.Category != "" { + ob.Category = o.Category + } else { + defaultCategory := "GENERAL" + ob.Category = &defaultCategory + } + for i := 0; i < len(o.Licenses); i++ { ob.Shortnames = append(ob.Shortnames, *o.Licenses[i].Shortname) } @@ -563,6 +602,7 @@ func (o *Obligation) UnmarshalJSON(data []byte) error { o.Comment = dto.Comment o.Active = dto.Active o.TextUpdatable = dto.TextUpdatable + o.Category = dto.Category if dto.Type != nil { o.Type = &ObligationType{ @@ -597,6 +637,7 @@ type ObligationDTO struct { Active *bool `json:"active"` TextUpdatable *bool `json:"text_updatable" example:"true"` Shortnames []string `json:"shortnames" validate:"required" example:"GPL-2.0-only,GPL-2.0-or-later"` + Category *string `json:"category" example:"DISTRIBUTION" validate:"required"` } // ObligationUpdateDTO represents an obligation json object. @@ -609,6 +650,7 @@ type ObligationUpdateDTO struct { Comment *string `json:"comment"` Active *bool `json:"active"` TextUpdatable *bool `json:"text_updatable" example:"true"` + Category *string `json:"category" example:"DISTRIBUTION"` } func (obDto *ObligationUpdateDTO) Converter() *Obligation { @@ -626,6 +668,7 @@ func (obDto *ObligationUpdateDTO) Converter() *Obligation { o.Comment = obDto.Comment o.Active = obDto.Active o.TextUpdatable = obDto.TextUpdatable + o.Category = obDto.Category return &o }