Skip to content

Commit

Permalink
feat: added new category field in obligations
Browse files Browse the repository at this point in the history
  • Loading branch information
Sourav Bhowmik authored and deo002 committed Feb 19, 2025
1 parent 556965f commit 58fc24c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 1 deletion.
21 changes: 21 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -2726,6 +2738,7 @@ const docTemplate = `{
"models.ObligationDTO": {
"type": "object",
"required": [
"category",
"classification",
"shortnames",
"text",
Expand All @@ -2736,6 +2749,10 @@ const docTemplate = `{
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down Expand Up @@ -2919,6 +2936,10 @@ const docTemplate = `{
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down
21 changes: 21 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down Expand Up @@ -2719,6 +2731,7 @@
"models.ObligationDTO": {
"type": "object",
"required": [
"category",
"classification",
"shortnames",
"text",
Expand All @@ -2729,6 +2742,10 @@
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down Expand Up @@ -2912,6 +2929,10 @@
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down
17 changes: 17 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -387,6 +397,9 @@ definitions:
properties:
active:
type: boolean
category:
example: DISTRIBUTION
type: string
classification:
example: GREEN
type: string
Expand Down Expand Up @@ -415,6 +428,7 @@ definitions:
example: RISK
type: string
required:
- category
- classification
- shortnames
- text
Expand Down Expand Up @@ -520,6 +534,9 @@ definitions:
properties:
active:
type: boolean
category:
example: DISTRIBUTION
type: string
classification:
example: GREEN
type: string
Expand Down
45 changes: 44 additions & 1 deletion pkg/models/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
"time"

"github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand All @@ -450,7 +474,7 @@ func (o *Obligation) BeforeCreate(tx *gorm.DB) (err error) {
o.Licenses[i] = &license
}

return
return nil
}

type ContextKey string
Expand Down Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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{
Expand Down Expand Up @@ -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.
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit 58fc24c

Please sign in to comment.