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

Balance #36

Merged
merged 10 commits into from
Oct 16, 2023
57 changes: 57 additions & 0 deletions extensions/label/label.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package label

func BlankMetadata() *Metadata {
return &Metadata{
Labels: []*Label{},
}
}

func (m *Metadata) MatchLabel(expectLabel *Label) bool {
for _, l := range m.GetLabels() {
if l.Value == expectLabel.Value && l.Key == expectLabel.Key {
return true
}
}
return false
}

func (m *Metadata) GetLabelByKey(key string) *Label {
for _, l := range m.GetLabels() {
if l.Key == key {
return l
}
}
return nil
}

func (m *Metadata) GetLabelsByKey(key string) []*Label {
var labels []*Label
for _, l := range m.GetLabels() {
if l.Key == key {
labels = append(labels, l)
}
}
return labels
}

func (m *Metadata) RemoveLabel(expectLabel *Label) bool {
var (
i int
l *Label
)
for i, l = range m.GetLabels() {
if l.Key == expectLabel.Key && l.Value == expectLabel.Value {
break
}
}
m.Labels = append(m.Labels[i:], m.Labels[i+1:]...)
return false
}

func (m *Metadata) AddLabel(newLabel *Label) error {
if err := newLabel.Validate(); err != nil {
return err
}
m.Labels = append(m.Labels, newLabel)
return nil
}
225 changes: 225 additions & 0 deletions extensions/label/label.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 18 additions & 0 deletions extensions/label/label.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
syntax = "proto3";

package extensions.label;

option go_package = "github.com/hyperledger-labs/cckit/extensions/label";

import "mwitkow/go-proto-validators/validator.proto";

// label - container for name/value content
message Label {
string key = 1 [(validator.field) = {length_gt: 1}];
string value = 2 [(validator.field) = {length_gt: 1}];
}

// Metadata - type for integrate to other objects
message Metadata {
repeated Label labels = 1;
}
49 changes: 49 additions & 0 deletions extensions/label/label.swagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"swagger": "2.0",
"info": {
"title": "label/label.proto",
"version": "version not set"
},
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"paths": {},
"definitions": {
"protobufAny": {
"type": "object",
"properties": {
"type_url": {
"type": "string"
},
"value": {
"type": "string",
"format": "byte"
}
}
},
"runtimeError": {
"type": "object",
"properties": {
"error": {
"type": "string"
},
"code": {
"type": "integer",
"format": "int32"
},
"message": {
"type": "string"
},
"details": {
"type": "array",
"items": {
"$ref": "#/definitions/protobufAny"
}
}
}
}
}
}
37 changes: 37 additions & 0 deletions extensions/label/label.validator.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading