Skip to content

Commit 8b7f470

Browse files
Create an FFEnum type and tag structure for OpenAPI annotations
Signed-off-by: Peter Broadhurst <peter.broadhurst@kaleido.io>
1 parent 6213b08 commit 8b7f470

File tree

15 files changed

+154
-69
lines changed

15 files changed

+154
-69
lines changed

internal/events/subscription_manager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ func TestDeleteDurableSubscriptionOk(t *testing.T) {
631631
}
632632

633633
mdi.On("GetSubscriptionByID", mock.Anything, subID).Return(subDef, nil)
634-
mdi.On("DeleteOffset", mock.Anything, fftypes.LowerCasedType("subscription"), subID.String()).Return(fmt.Errorf("this error is logged and swallowed"))
634+
mdi.On("DeleteOffset", mock.Anything, fftypes.FFEnum("subscription"), subID.String()).Return(fmt.Errorf("this error is logged and swallowed"))
635635
sm.deletedDurableSubscription(subID)
636636

637637
assert.Empty(t, sm.connections["conn1"].dispatchers)

internal/events/webhooks/webhooks.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,9 @@ func (wh *WebHooks) doDelivery(connID string, reply bool, sub *fftypes.Subscript
364364

365365
// Emit the response
366366
if reply {
367-
txType := fftypes.LowerCasedType(strings.ToLower(sub.Options.TransportOptions().GetString("replytx")))
367+
txType := fftypes.FFEnum(strings.ToLower(sub.Options.TransportOptions().GetString("replytx")))
368368
if req != nil && req.replyTx != "" {
369-
txType = fftypes.LowerCasedType(strings.ToLower(req.replyTx))
369+
txType = fftypes.FFEnum(strings.ToLower(req.replyTx))
370370
}
371371
wh.callbacks.DeliveryResponse(connID, &fftypes.EventDeliveryResponse{
372372
ID: event.ID,

internal/tokens/https/https.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (h *HTTPS) handleTokenPoolCreate(ctx context.Context, data fftypes.JSONObje
154154
},
155155
Namespace: ns,
156156
Name: name,
157-
Type: fftypes.LowerCasedType(tokenType),
157+
Type: fftypes.FFEnum(tokenType),
158158
ProtocolID: protocolID,
159159
}
160160

pkg/fftypes/datatype.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,20 +22,20 @@ import (
2222
"github.com/hyperledger-labs/firefly/internal/i18n"
2323
)
2424

25-
type ValidatorType = LowerCasedType
25+
type ValidatorType = FFEnum
2626

27-
const (
27+
var (
2828
// ValidatorTypeJSON is the validator type for JSON Schema validation
29-
ValidatorTypeJSON ValidatorType = "json"
29+
ValidatorTypeJSON ValidatorType = ffEnum("validatortype", "json")
3030
// ValidatorTypeSystemDefinition is the validator type for system definitions
31-
ValidatorTypeSystemDefinition ValidatorType = "definition"
31+
ValidatorTypeSystemDefinition ValidatorType = ffEnum("validatortype", "definition")
3232
)
3333

3434
// Datatype is the structure defining a data definition, such as a JSON schema
3535
type Datatype struct {
3636
ID *UUID `json:"id,omitempty"`
3737
Message *UUID `json:"message,omitempty"`
38-
Validator ValidatorType `json:"validator"`
38+
Validator ValidatorType `json:"validator" ffenum:"validatortype"`
3939
Namespace string `json:"namespace,omitempty"`
4040
Name string `json:"name,omitempty"`
4141
Version string `json:"version,omitempty"`

pkg/fftypes/enum.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright © 2021 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package fftypes
18+
19+
import (
20+
"database/sql/driver"
21+
"strings"
22+
)
23+
24+
type FFEnum string
25+
26+
var enumValues = map[string][]FFEnum{}
27+
28+
func ffEnum(t string, val FFEnum) FFEnum {
29+
enumValues[t] = append(enumValues[t], val)
30+
return val
31+
}
32+
33+
func FFEnumValues(t string) []FFEnum {
34+
return enumValues[t]
35+
}
36+
37+
func (ts FFEnum) String() string {
38+
return strings.ToLower(string(ts))
39+
}
40+
41+
func (ts FFEnum) Lower() FFEnum {
42+
return FFEnum(strings.ToLower(string(ts)))
43+
}
44+
45+
func (ts FFEnum) Equals(ts2 FFEnum) bool {
46+
return strings.EqualFold(string(ts), string(ts2))
47+
}
48+
49+
func (ts FFEnum) Value() (driver.Value, error) {
50+
return ts.String(), nil
51+
}
52+
53+
func (ts *FFEnum) UnmarshalText(b []byte) error {
54+
*ts = FFEnum(strings.ToLower(string(b)))
55+
return nil
56+
}

pkg/fftypes/enum_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// Copyright © 2021 Kaleido, Inc.
2+
//
3+
// SPDX-License-Identifier: Apache-2.0
4+
//
5+
// Licensed under the Apache License, Version 2.0 (the "License");
6+
// you may not use this file except in compliance with the License.
7+
// You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing, software
12+
// distributed under the License is distributed on an "AS IS" BASIS,
13+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
// See the License for the specific language governing permissions and
15+
// limitations under the License.
16+
17+
package fftypes
18+
19+
import (
20+
"testing"
21+
22+
"github.com/stretchr/testify/assert"
23+
)
24+
25+
func TestFFEnumValues(t *testing.T) {
26+
assert.Equal(t, FFEnum("test1"), ffEnum("ut", "test1"))
27+
assert.Equal(t, FFEnum("test2"), ffEnum("ut", "test2"))
28+
assert.Equal(t, []FFEnum{"test1", "test2"}, FFEnumValues("ut"))
29+
}

pkg/fftypes/event.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,31 @@
1717
package fftypes
1818

1919
// EventType indicates what the event means, as well as what the Reference in the event refers to
20-
type EventType = LowerCasedType
20+
type EventType = FFEnum
2121

22-
const (
22+
var (
2323
// EventTypeMessageConfirmed is the most important event type in the system. This means a message and all of its data
2424
// is available for processing by an application. Most applications only need to listen to this event type
25-
EventTypeMessageConfirmed EventType = "message_confirmed"
25+
EventTypeMessageConfirmed EventType = ffEnum("eventtype", "message_confirmed")
2626
// EventTypeMessageRejected occurs if a message is received and confirmed from a sequencing perspective, but is rejected as invalid (mismatch to schema, or duplicate system broadcast)
27-
EventTypeMessageRejected EventType = "message_rejected"
27+
EventTypeMessageRejected EventType = ffEnum("eventtype", "message_rejected")
2828
// EventTypeNamespaceConfirmed occurs when a new namespace is ready for use (on the namespace itself)
29-
EventTypeNamespaceConfirmed EventType = "namespace_confirmed"
29+
EventTypeNamespaceConfirmed EventType = ffEnum("eventtype", "namespace_confirmed")
3030
// EventTypeDatatypeConfirmed occurs when a new datatype is ready for use (on the namespace of the datatype)
31-
EventTypeDatatypeConfirmed EventType = "datatype_confirmed"
31+
EventTypeDatatypeConfirmed EventType = ffEnum("eventtype", "datatype_confirmed")
3232
// EventTypeGroupConfirmed occurs when a new group is ready to use (on the namespace of the group, on all group participants)
33-
EventTypeGroupConfirmed EventType = "group_confirmed"
33+
EventTypeGroupConfirmed EventType = ffEnum("eventtype", "group_confirmed")
3434
// EventTypePoolConfirmed occurs when a new token pool is ready for use
35-
EventTypePoolConfirmed EventType = "token_pool_confirmed"
35+
EventTypePoolConfirmed EventType = ffEnum("eventtype", "token_pool_confirmed")
3636
// EventTypePoolRejected occurs when a new token pool is rejected (due to validation errors, duplicates, etc)
37-
EventTypePoolRejected EventType = "token_pool_rejected"
37+
EventTypePoolRejected EventType = ffEnum("eventtype", "token_pool_rejected")
3838
)
3939

4040
// Event is an activity in the system, delivered reliably to applications, that indicates something has happened in the network
4141
type Event struct {
4242
ID *UUID `json:"id"`
4343
Sequence int64 `json:"sequence"`
44-
Type EventType `json:"type"`
44+
Type EventType `json:"type" ffenum:"eventtype"`
4545
Namespace string `json:"namespace"`
4646
Reference *UUID `json:"reference"`
4747
Created *FFTime `json:"created"`

pkg/fftypes/message.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,25 +30,25 @@ const (
3030
)
3131

3232
// MessageType is the fundamental type of a message
33-
type MessageType = LowerCasedType
33+
type MessageType = FFEnum
3434

35-
const (
35+
var (
3636
// MessageTypeDefinition is a message broadcasting a definition of a system type, pre-defined by firefly (namespaces, members, data definitions, etc.)
37-
MessageTypeDefinition MessageType = "definition"
37+
MessageTypeDefinition MessageType = ffEnum("messagetype", "definition")
3838
// MessageTypeBroadcast is a broadcast message, meaning it is intended to be visible by all parties in the network
39-
MessageTypeBroadcast MessageType = "broadcast"
39+
MessageTypeBroadcast MessageType = ffEnum("messagetype", "broadcast")
4040
// MessageTypePrivate is a private message, meaning it is only sent explicitly to individual parties in the network
41-
MessageTypePrivate MessageType = "private"
41+
MessageTypePrivate MessageType = ffEnum("messagetype", "private")
4242
// MessageTypeGroupInit is a special private message that contains the definition of the group
43-
MessageTypeGroupInit MessageType = "groupinit"
43+
MessageTypeGroupInit MessageType = ffEnum("messagetype", "groupinit")
4444
)
4545

4646
// MessageHeader contains all fields that contribute to the hash
4747
// The order of the serialization mut not change, once released
4848
type MessageHeader struct {
4949
ID *UUID `json:"id,omitempty"`
5050
CID *UUID `json:"cid,omitempty"`
51-
Type MessageType `json:"type"`
51+
Type MessageType `json:"type" ffenum:"messagetype"`
5252
TxType TransactionType `json:"txtype,omitempty"`
5353
Author string `json:"author,omitempty"`
5454
Created *FFTime `json:"created,omitempty"`

pkg/fftypes/namespace.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ import (
2424
)
2525

2626
// NamespaceType describes when the namespace was created from local configuration, or broadcast through the network
27-
type NamespaceType = LowerCasedType
27+
type NamespaceType = FFEnum
2828

29-
const (
29+
var (
3030
// NamespaceTypeLocal is a namespace that only exists because it was defined in the local configuration of the node
31-
NamespaceTypeLocal NamespaceType = "local"
31+
NamespaceTypeLocal NamespaceType = ffEnum("namespacetype", "local")
3232
// NamespaceTypeBroadcast is a namespace that was broadcast through the network. Broadcast namespaces can overwrite a local namespace
33-
NamespaceTypeBroadcast NamespaceType = "broadcast"
33+
NamespaceTypeBroadcast NamespaceType = ffEnum("namespacetype", "broadcast")
3434
// NamespaceTypeSystem is a reserved namespace used by FireFly itself
35-
NamespaceTypeSystem NamespaceType = "system"
35+
NamespaceTypeSystem NamespaceType = ffEnum("namespacetype", "system")
3636
)
3737

3838
// Namespace is a isolate set of named resources, to allow multiple applications to co-exist in the same network, with the same named objects.
@@ -42,7 +42,7 @@ type Namespace struct {
4242
Message *UUID `json:"message,omitempty"`
4343
Name string `json:"name"`
4444
Description string `json:"description"`
45-
Type NamespaceType `json:"type"`
45+
Type NamespaceType `json:"type" ffenum:"namespacetype"`
4646
Created *FFTime `json:"created"`
4747
}
4848

pkg/fftypes/offset.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@
1616

1717
package fftypes
1818

19-
type OffsetType = LowerCasedType
19+
type OffsetType = FFEnum
2020

21-
const (
21+
var (
2222
// OffsetTypeBatch is an offset stored by the batch manager on the messages table
23-
OffsetTypeBatch OffsetType = "batch"
23+
OffsetTypeBatch OffsetType = ffEnum("offsettype", "batch")
2424
// OffsetTypeAggregator is an offset stored by the aggregator on the events table
25-
OffsetTypeAggregator OffsetType = "aggregator"
25+
OffsetTypeAggregator OffsetType = ffEnum("offsettype", "aggregator")
2626
// OffsetTypeSubscription is an offeset stored by a dispatcher on the events table
27-
OffsetTypeSubscription OffsetType = "subscription"
27+
OffsetTypeSubscription OffsetType = ffEnum("offsettype", "subscription")
2828
)
2929

3030
// Offset is a simple stored data structure that records a sequence position within another collection
3131
type Offset struct {
32-
Type OffsetType `json:"type"`
32+
Type OffsetType `json:"type" ffenum:"offsettype"`
3333
Name string `json:"name"`
3434
Current int64 `json:"current,omitempty"`
3535

0 commit comments

Comments
 (0)