From a4f2602f585a3c2995a0abea7010b333631341d9 Mon Sep 17 00:00:00 2001 From: Tim Rozet Date: Wed, 4 Jan 2023 10:30:09 -0500 Subject: [PATCH] Expose getter functions for fields with modelgen Exposes a get method that is useful when using generic types or interfaces across many models. Signed-off-by: Tim Rozet --- modelgen/table.go | 9 +++-- modelgen/table_test.go | 72 ++++++++++++++++++++++++++++++++++++++ ovsdb/serverdb/database.go | 36 +++++++++++++++++++ 3 files changed, 115 insertions(+), 2 deletions(-) diff --git a/modelgen/table.go b/modelgen/table.go index 8dd5d667..fa2dcb76 100644 --- a/modelgen/table.go +++ b/modelgen/table.go @@ -34,7 +34,12 @@ import "github.com/ovn-org/libovsdb/model" {{- else }} {{- $type = FieldType $tableName $field.Column $field.Schema }} {{- end }} -{{- if or (eq (index $type 0) '*') (eq (slice $type 0 2) "[]") (eq (slice $type 0 3) "map") }} + +func (a *{{ $structName }}) Get{{ $fieldName }}() {{ $type }} { + return a.{{ $fieldName }} +} + +{{ if or (eq (index $type 0) '*') (eq (slice $type 0 2) "[]") (eq (slice $type 0 3) "map") }} func copy{{ $structName }}{{ $fieldName }}(a {{ $type }}) {{ $type }} { if a == nil { return nil @@ -88,7 +93,7 @@ func equal{{ $structName }}{{ $fieldName }}(a, b {{ $type }}) bool { } {{ end }} -{{- end }} +{{ end }} func (a *{{ $structName }}) DeepCopyInto(b *{{ $structName }}) { *b = *a diff --git a/modelgen/table_test.go b/modelgen/table_test.go index e4172706..ef0d113e 100644 --- a/modelgen/table_test.go +++ b/modelgen/table_test.go @@ -189,6 +189,26 @@ type AtomicTable struct { Str string ` + "`" + `ovsdb:"str"` + "`" + ` } +func (a *AtomicTable) GetUUID() string { + return a.UUID +} + +func (a *AtomicTable) GetEventType() AtomicTableEventType { + return a.EventType +} + +func (a *AtomicTable) GetFloat() float64 { + return a.Float +} + +func (a *AtomicTable) GetInt() int { + return a.Int +} + +func (a *AtomicTable) GetProtocol() *AtomicTableProtocol { + return a.Protocol +} + func copyAtomicTableProtocol(a *AtomicTableProtocol) *AtomicTableProtocol { if a == nil { return nil @@ -207,6 +227,10 @@ func equalAtomicTableProtocol(a, b *AtomicTableProtocol) bool { return *a == *b } +func (a *AtomicTable) GetStr() string { + return a.Str +} + func (a *AtomicTable) DeepCopyInto(b *AtomicTable) { *b = *a b.Protocol = copyAtomicTableProtocol(a.Protocol) @@ -352,6 +376,26 @@ func (a *AtomicTable) PrintAtomicTableOtherProtocol() bool { fmt.Printf(a.OtherProtocol) } +func (a *AtomicTable) GetUUID() string { + return a.UUID +} + +func (a *AtomicTable) GetEventType() AtomicTableEventType { + return a.EventType +} + +func (a *AtomicTable) GetFloat() float64 { + return a.Float +} + +func (a *AtomicTable) GetInt() int { + return a.Int +} + +func (a *AtomicTable) GetProtocol() *AtomicTableProtocol { + return a.Protocol +} + func copyAtomicTableProtocol(a *AtomicTableProtocol) *AtomicTableProtocol { if a == nil { return nil @@ -370,6 +414,10 @@ func equalAtomicTableProtocol(a, b *AtomicTableProtocol) bool { return *a == *b } +func (a *AtomicTable) GetStr() string { + return a.Str +} + func (a *AtomicTable) DeepCopyInto(b *AtomicTable) { *b = *a b.Protocol = copyAtomicTableProtocol(a.Protocol) @@ -435,6 +483,26 @@ type AtomicTable struct { Str string ` + "`" + `ovsdb:"str"` + "`" + ` } +func (a *AtomicTable) GetUUID() string { + return a.UUID +} + +func (a *AtomicTable) GetEventType() string { + return a.EventType +} + +func (a *AtomicTable) GetFloat() float64 { + return a.Float +} + +func (a *AtomicTable) GetInt() int { + return a.Int +} + +func (a *AtomicTable) GetProtocol() *string { + return a.Protocol +} + func copyAtomicTableProtocol(a *string) *string { if a == nil { return nil @@ -453,6 +521,10 @@ func equalAtomicTableProtocol(a, b *string) bool { return *a == *b } +func (a *AtomicTable) GetStr() string { + return a.Str +} + func (a *AtomicTable) DeepCopyInto(b *AtomicTable) { *b = *a b.Protocol = copyAtomicTableProtocol(a.Protocol) diff --git a/ovsdb/serverdb/database.go b/ovsdb/serverdb/database.go index 9ca712a7..274a7164 100644 --- a/ovsdb/serverdb/database.go +++ b/ovsdb/serverdb/database.go @@ -30,6 +30,14 @@ type Database struct { Sid *string `ovsdb:"sid"` } +func (a *Database) GetUUID() string { + return a.UUID +} + +func (a *Database) GetCid() *string { + return a.Cid +} + func copyDatabaseCid(a *string) *string { if a == nil { return nil @@ -48,6 +56,14 @@ func equalDatabaseCid(a, b *string) bool { return *a == *b } +func (a *Database) GetConnected() bool { + return a.Connected +} + +func (a *Database) GetIndex() *int { + return a.Index +} + func copyDatabaseIndex(a *int) *int { if a == nil { return nil @@ -66,6 +82,22 @@ func equalDatabaseIndex(a, b *int) bool { return *a == *b } +func (a *Database) GetLeader() bool { + return a.Leader +} + +func (a *Database) GetModel() DatabaseModel { + return a.Model +} + +func (a *Database) GetName() string { + return a.Name +} + +func (a *Database) GetSchema() *string { + return a.Schema +} + func copyDatabaseSchema(a *string) *string { if a == nil { return nil @@ -84,6 +116,10 @@ func equalDatabaseSchema(a, b *string) bool { return *a == *b } +func (a *Database) GetSid() *string { + return a.Sid +} + func copyDatabaseSid(a *string) *string { if a == nil { return nil