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 committed Nov 18, 2024
1 parent d3058ba commit d2d163d
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
24 changes: 24 additions & 0 deletions cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2459,6 +2459,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 @@ -2538,6 +2550,7 @@ const docTemplate = `{
"models.ObligationDTO": {
"type": "object",
"required": [
"category",
"classification",
"shortnames",
"text",
Expand All @@ -2548,6 +2561,10 @@ const docTemplate = `{
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down Expand Up @@ -2727,10 +2744,17 @@ const docTemplate = `{
},
"models.ObligationUpdateDTO": {
"type": "object",
"required": [
"category"
],
"properties": {
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down
24 changes: 24 additions & 0 deletions cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2452,6 +2452,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 @@ -2531,6 +2543,7 @@
"models.ObligationDTO": {
"type": "object",
"required": [
"category",
"classification",
"shortnames",
"text",
Expand All @@ -2541,6 +2554,10 @@
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down Expand Up @@ -2720,10 +2737,17 @@
},
"models.ObligationUpdateDTO": {
"type": "object",
"required": [
"category"
],
"properties": {
"active": {
"type": "boolean"
},
"category": {
"type": "string",
"example": "DISTRIBUTION"
},
"classification": {
"type": "string",
"example": "GREEN"
Expand Down
19 changes: 19 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 All @@ -537,6 +554,8 @@ definitions:
type:
example: RISK
type: string
required:
- category
type: object
models.PaginationMeta:
properties:
Expand Down
41 changes: 41 additions & 0 deletions 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 @@ -323,6 +324,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 @@ -380,6 +400,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 Down Expand Up @@ -453,6 +477,11 @@ func (o *Obligation) BeforeUpdate(tx *gorm.DB) (err error) {
return fmt.Errorf("obligation classification must be one of the following values:%s", allClassifications)
}
}

if err := validateCategory(o); err != nil {
return err
}

return
}

Expand All @@ -466,6 +495,7 @@ func (o *Obligation) MarshalJSON() ([]byte, error) {
Active: o.Active,
TextUpdatable: o.TextUpdatable,
Shortnames: []string{},
Category: o.Category,
}

if o.Type != nil {
Expand All @@ -476,6 +506,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 @@ -501,6 +538,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 @@ -535,6 +573,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 @@ -547,6 +586,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" validate:"required"`
}

func (obDto *ObligationUpdateDTO) Converter() *Obligation {
Expand All @@ -564,6 +604,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 d2d163d

Please sign in to comment.