Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added new category field in obligations #93

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 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 @@ -2915,10 +2932,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 @@ -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 @@ -2908,10 +2925,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 @@ -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 Down Expand Up @@ -515,6 +539,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 o.Category != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you return error if o.Category is not nil?

You need to validate category field if it's not empty. It might be nil as it's a PATCH request.

return err
}

return
}

Expand All @@ -528,6 +557,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 +568,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 +600,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 +635,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 +648,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 @@ -626,6 +666,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