Skip to content
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
34 changes: 33 additions & 1 deletion cmd/laas/docs/docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2475,6 +2475,9 @@ const docTemplate = `{
"datatypes.JSONType-models_LicenseDBSchemaExtension": {
"type": "object"
},
"datatypes.JSONType-models_ObligationSchemaExtension": {
"type": "object"
},
"models.APICollection": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2825,7 +2828,15 @@ const docTemplate = `{
}
},
"models.LicenseDBSchemaExtension": {
"type": "object"
"type": "object",
"properties": {
"license_explanation": {
"type": "string"
},
"license_suffix": {
"type": "string"
}
}
},
"models.LicenseError": {
"type": "object",
Expand Down Expand Up @@ -3089,6 +3100,9 @@ const docTemplate = `{
"comment": {
"type": "string"
},
"externalRef": {
Copy link
Member

Choose a reason for hiding this comment

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

Can we bring more consistency in naming? I see there is externalRef in json and external_ref in Go Structs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes Kaushl, This is the schema for Go struct in swagger. In json, it's external_ref.

"$ref": "#/definitions/datatypes.JSONType-models_ObligationSchemaExtension"
},
"id": {
"type": "integer"
},
Expand Down Expand Up @@ -3184,6 +3198,9 @@ const docTemplate = `{
"comment": {
"type": "string"
},
"external_ref": {
"$ref": "#/definitions/models.ObligationSchemaExtension"
},
"modifications": {
"type": "boolean",
"example": true
Expand Down Expand Up @@ -3324,6 +3341,17 @@ const docTemplate = `{
}
}
},
"models.ObligationSchemaExtension": {
"type": "object",
"properties": {
"obligation_explanation": {
"type": "string"
},
"obligation_suffix": {
"type": "string"
}
}
},
"models.ObligationType": {
"type": "object",
"required": [
Expand Down Expand Up @@ -3371,6 +3399,10 @@ const docTemplate = `{
"comment": {
"type": "string"
},
"external_ref": {
"type": "object",
"additionalProperties": true
},
"modifications": {
"type": "boolean",
"example": true
Expand Down
34 changes: 33 additions & 1 deletion cmd/laas/docs/swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,9 @@
"datatypes.JSONType-models_LicenseDBSchemaExtension": {
"type": "object"
},
"datatypes.JSONType-models_ObligationSchemaExtension": {
"type": "object"
},
"models.APICollection": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -2818,7 +2821,15 @@
}
},
"models.LicenseDBSchemaExtension": {
"type": "object"
"type": "object",
"properties": {
"license_explanation": {
"type": "string"
},
"license_suffix": {
"type": "string"
}
}
},
"models.LicenseError": {
"type": "object",
Expand Down Expand Up @@ -3082,6 +3093,9 @@
"comment": {
"type": "string"
},
"externalRef": {
"$ref": "#/definitions/datatypes.JSONType-models_ObligationSchemaExtension"
},
"id": {
"type": "integer"
},
Expand Down Expand Up @@ -3177,6 +3191,9 @@
"comment": {
"type": "string"
},
"external_ref": {
"$ref": "#/definitions/models.ObligationSchemaExtension"
},
"modifications": {
"type": "boolean",
"example": true
Expand Down Expand Up @@ -3317,6 +3334,17 @@
}
}
},
"models.ObligationSchemaExtension": {
"type": "object",
"properties": {
"obligation_explanation": {
"type": "string"
},
"obligation_suffix": {
"type": "string"
}
}
},
"models.ObligationType": {
"type": "object",
"required": [
Expand Down Expand Up @@ -3364,6 +3392,10 @@
"comment": {
"type": "string"
},
"external_ref": {
"type": "object",
"additionalProperties": true
},
"modifications": {
"type": "boolean",
"example": true
Expand Down
21 changes: 21 additions & 0 deletions cmd/laas/docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ basePath: /api/v1
definitions:
datatypes.JSONType-models_LicenseDBSchemaExtension:
type: object
datatypes.JSONType-models_ObligationSchemaExtension:
type: object
models.APICollection:
properties:
authenticated:
Expand Down Expand Up @@ -245,6 +247,11 @@ definitions:
type: integer
type: object
models.LicenseDBSchemaExtension:
properties:
license_explanation:
type: string
license_suffix:
type: string
type: object
models.LicenseError:
properties:
Expand Down Expand Up @@ -431,6 +438,8 @@ definitions:
$ref: '#/definitions/models.ObligationClassification'
comment:
type: string
externalRef:
$ref: '#/definitions/datatypes.JSONType-models_ObligationSchemaExtension'
id:
type: integer
licenses:
Expand Down Expand Up @@ -490,6 +499,8 @@ definitions:
type: string
comment:
type: string
external_ref:
$ref: '#/definitions/models.ObligationSchemaExtension'
modifications:
example: true
type: boolean
Expand Down Expand Up @@ -595,6 +606,13 @@ definitions:
example: 200
type: integer
type: object
models.ObligationSchemaExtension:
properties:
obligation_explanation:
type: string
obligation_suffix:
type: string
type: object
models.ObligationType:
properties:
type:
Expand Down Expand Up @@ -627,6 +645,9 @@ definitions:
type: string
comment:
type: string
external_ref:
additionalProperties: true
type: object
modifications:
example: true
type: boolean
Expand Down
44 changes: 37 additions & 7 deletions cmd/laas/gen_external_ref_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ type ExternalRefFields struct {
Fields []ExternalRefFieldMetaData `yaml:"fields"`
}

type ExternalRefYAML struct {
License ExternalRefFields `yaml:"license"`
Obligation ExternalRefFields `yaml:"obligation"`
}

func main() {
externalRefFields := ExternalRefFields{}
externalRefYAML := ExternalRefYAML{}

fieldsMetadata, err := os.ReadFile(PATH_EXTERNAL_REF_CONFIG_FILE)
if err != nil {
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
}

err = yaml.Unmarshal(fieldsMetadata, &externalRefFields)
err = yaml.Unmarshal(fieldsMetadata, &externalRefYAML)
if err != nil {
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
}
Expand All @@ -56,9 +61,34 @@ func main() {

// REUSE-IgnoreStart

var fields []jen.Code
var licenseFields, obligationFields []jen.Code

for _, f := range externalRefYAML.License.Fields {
field := jen.Id(f.StructFieldName).Op("*")
if f.StructFieldName == "" {
err = errors.New("field struct_field_name is missing in external_ref_fields.yaml")
}
switch f.Type {
case "boolean":
field = field.Bool()
case "string":
field = field.String()
case "int":
field = field.Int64()
default:
err = fmt.Errorf("type %s in external_ref_fields.yaml is not supported", f.Type)
}
if err != nil {
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
return
}
field = field.Tag(map[string]string{"json": fmt.Sprintf("%s,omitempty", f.Name)})
licenseFields = append(licenseFields, field)
}

f.Type().Id("LicenseDBSchemaExtension").Struct(licenseFields...)

for _, f := range externalRefFields.Fields {
for _, f := range externalRefYAML.Obligation.Fields {
field := jen.Id(f.StructFieldName).Op("*")
if f.StructFieldName == "" {
err = errors.New("field struct_field_name is missing in external_ref_fields.yaml")
Expand All @@ -77,11 +107,11 @@ func main() {
log.Fatalf("Failed to instantiate json schema for external ref in license: %v", err)
return
}
field = field.Tag(map[string]string{"json": fmt.Sprintf("%s,omitempty", f.Name), "swaggerignore": "true"})
fields = append(fields, field)
field = field.Tag(map[string]string{"json": fmt.Sprintf("%s,omitempty", f.Name)})
obligationFields = append(obligationFields, field)
}

f.Type().Id("LicenseDBSchemaExtension").Struct(fields...)
f.Type().Id("ObligationSchemaExtension").Struct(obligationFields...)
f.Save(PATH_EXTERNAL_REF_STRUCT_FILE)

}
42 changes: 29 additions & 13 deletions external_ref_fields.example.yaml
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
# SPDX-License-Identifier: GPL-2.0-only
# SPDX-FileCopyrightText: FOSSology contributors

fields:
- name: "license_suffix"
type: "string"
struct_field_name: "LicenseSuffix"
label: "License Suffix"
formComponentPath: "../components/dynamic/inputField"
componentType: "input"
- name: "license_explanation"
type: "string"
struct_field_name: "LicenseExplanation"
label: "License Explanation"
formComponentPath: "../components/dynamic/inputField"
componentType: "input"
license:
fields:
- name: "license_suffix"
type: "string"
struct_field_name: "LicenseSuffix"
label: "License Suffix"
formComponentPath: "../components/dynamic/inputField"
componentType: "input"
- name: "license_explanation"
type: "string"
struct_field_name: "LicenseExplanation"
label: "License Explanation"
formComponentPath: "../components/dynamic/inputField"
componentType: "input"

obligation:
fields:
- name: "obligation_suffix"
type: "string"
struct_field_name: "ObligationSuffix"
label: "Obligation Suffix"
formComponentPath: "../components/dynamic/inputField"
componentType: "input"
- name: "obligation_explanation"
type: "string"
struct_field_name: "ObligationExplanation"
label: "Obligation Explanation"
formComponentPath: "../components/dynamic/inputField"
componentType: "input"
Loading
Loading