From 49e4d26c36376e6ea4ef2e57d9fc89c5df29c4be Mon Sep 17 00:00:00 2001 From: Manel Montilla Date: Wed, 20 Dec 2023 16:39:49 +0100 Subject: [PATCH] WIP --- go.mod | 3 ++- pkg/asyncapi/_gen/package.json | 2 +- pkg/asyncapi/models.go | 45 +++++++++++++++++++++++++--------- pkg/asyncapi/vulcan.go | 7 +++--- pkg/asyncapi/vulcan_test.go | 4 +-- 5 files changed, 42 insertions(+), 19 deletions(-) diff --git a/go.mod b/go.mod index 4f1721ca..e5624012 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,7 @@ module github.com/adevinta/vulcan-api -go 1.21.0 +go 1.21.2 + toolchain go1.21.5 require ( diff --git a/pkg/asyncapi/_gen/package.json b/pkg/asyncapi/_gen/package.json index 59cd4f5e..5924398b 100644 --- a/pkg/asyncapi/_gen/package.json +++ b/pkg/asyncapi/_gen/package.json @@ -6,7 +6,7 @@ "author": "", "type": "module", "dependencies": { - "@asyncapi/modelina": "^0.59.2", + "@asyncapi/modelina": "^1.0.0", "@asyncapi/parser": "^1.15.1" } } diff --git a/pkg/asyncapi/models.go b/pkg/asyncapi/models.go index b9f64829..87992689 100644 --- a/pkg/asyncapi/models.go +++ b/pkg/asyncapi/models.go @@ -13,7 +13,7 @@ type AssetPayload struct { Scannable bool AssetType *AssetType Identifier string - Annotations []*Annotation + Annotations []interface{} } // Team represents a Team model. @@ -24,21 +24,42 @@ type Team struct { Tag string } -// AssetType represents an enum of string. -type AssetType string +// AssetType represents an enum of AssetType. +type AssetType uint const ( - AssetTypeIp AssetType = "IP" - AssetTypeDomainName = "DomainName" - AssetTypeHostname = "Hostname" - AssetTypeAwsAccount = "AWSAccount" - AssetTypeIpRange = "IPRange" - AssetTypeDockerImage = "DockerImage" - AssetTypeWebAddress = "WebAddress" - AssetTypeGitRepository = "GitRepository" - AssetTypeGcpProject = "GCPProject" + AssetTypeIp AssetType = iota + AssetTypeDomainName + AssetTypeHostname + AssetTypeAwsAccount + AssetTypeIpRange + AssetTypeDockerImage + AssetTypeWebAddress + AssetTypeGitRepository + AssetTypeGcpProject ) +// Value returns the value of the enum. +func (op AssetType) Value() any { + if op >= AssetType(len(AssetTypeValues)) { + return nil + } + return AssetTypeValues[op] +} + +var AssetTypeValues = []any{"IP", "DomainName", "Hostname", "AWSAccount", "IPRange", "DockerImage", "WebAddress", "GitRepository", "GCPProject"} +var ValuesToAssetType = map[any]AssetType{ + AssetTypeValues[AssetTypeIp]: AssetTypeIp, + AssetTypeValues[AssetTypeDomainName]: AssetTypeDomainName, + AssetTypeValues[AssetTypeHostname]: AssetTypeHostname, + AssetTypeValues[AssetTypeAwsAccount]: AssetTypeAwsAccount, + AssetTypeValues[AssetTypeIpRange]: AssetTypeIpRange, + AssetTypeValues[AssetTypeDockerImage]: AssetTypeDockerImage, + AssetTypeValues[AssetTypeWebAddress]: AssetTypeWebAddress, + AssetTypeValues[AssetTypeGitRepository]: AssetTypeGitRepository, + AssetTypeValues[AssetTypeGcpProject]: AssetTypeGcpProject, +} + // Annotation represents a Annotation model. type Annotation struct { Key string diff --git a/pkg/asyncapi/vulcan.go b/pkg/asyncapi/vulcan.go index 0a7df49f..ebdbb52b 100644 --- a/pkg/asyncapi/vulcan.go +++ b/pkg/asyncapi/vulcan.go @@ -106,7 +106,7 @@ func (v *Vulcan) DeleteAsset(asset AssetPayload) error { // NullVulcan implements an Async Vulcan API interface that does not send the // events to any [EventStreamClient]. It's intended to be used when the async -// API is disabled but other components still need to fullfill a dependency +// API is disabled but other components still need to fullfil a dependency // with the Vulcan Async Server. type NullVulcan struct { } @@ -124,10 +124,11 @@ func (v *NullVulcan) PushAsset(asset AssetPayload) error { } func metadata(asset AssetPayload) map[string][]byte { - // The asset type can't be nil. + // The asset type can't be nil and its value is a string. + at := asset.AssetType.Value().(string) return map[string][]byte{ "identifier": []byte(asset.Identifier), - "type": []byte(*asset.AssetType), + "type": []byte(at), "version": []byte(Version), } } diff --git a/pkg/asyncapi/vulcan_test.go b/pkg/asyncapi/vulcan_test.go index 4db79a31..e996e460 100644 --- a/pkg/asyncapi/vulcan_test.go +++ b/pkg/asyncapi/vulcan_test.go @@ -17,7 +17,7 @@ var assetFixtures = map[string]AssetPayload{ "Asset1": { Id: "Asset1", Identifier: "example.com", - AssetType: (*AssetType)(strToPtr(AssetTypeDomainName)), + AssetType: assettypeToPtr(AssetTypeDomainName), Team: &Team{ Id: "Team1", Name: "Team1", @@ -117,6 +117,6 @@ func mustJSONMarshal(assset AssetPayload) []byte { return content } -func strToPtr(v string) *string { +func assettypeToPtr(v AssetType) *AssetType { return &v }