Skip to content

Commit

Permalink
Merge pull request #157 from dave-tucker/moar-linting
Browse files Browse the repository at this point in the history
Moar linting
  • Loading branch information
dave-tucker authored Jun 11, 2021
2 parents 87c3031 + 8d373f9 commit e6caf40
Show file tree
Hide file tree
Showing 34 changed files with 718 additions and 537 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.39
version: v1.40.1

- name: Check Formatting
run: make fmt
Expand Down
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ linters:
enable:
- deadcode
- errcheck
- gocyclo
- goimports
- gosimple
- govet
- ineffassign
- misspell
- revive
- staticcheck
- structcheck
- typecheck
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
libovsdb
========

[![libovsb-ci](https://github.com/ovn-org/libovsdb/actions/workflows/ci.yml/badge.svg)](https://github.com/ovn-org/libovsdb/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/ovn-org/libovsdb/badge.svg?branch=main)](https://coveralls.io/github/ovn-org/libovsdb?branch=main)
[![libovsb-ci](https://github.com/ovn-org/libovsdb/actions/workflows/ci.yml/badge.svg)](https://github.com/ovn-org/libovsdb/actions/workflows/ci.yml) [![Coverage Status](https://coveralls.io/repos/github/ovn-org/libovsdb/badge.svg?branch=main)](https://coveralls.io/github/ovn-org/libovsdb?branch=main) [![Go Report Card](https://goreportcard.com/badge/github.com/ovn-org/libovsdb)](https://goreportcard.com/report/github.com/ovn-org/libovsdb)

An OVSDB Library written in Go

Expand Down Expand Up @@ -69,11 +69,11 @@ create an equality condition (using `ovsdb.ConditionEqual`) on the `_uuid` field
return strings.HasPrefix(ls.Name, "ext_")
}).List(&lsList)

The table is infered from the type that the function accepts as only argument.
The table is inferred from the type that the function accepts as only argument.

## Documentation

This package is divided into several subpackages. Documentation for each subpackage is available at [pkg.go.dev][doc]:
This package is divided into several sub-packages. Documentation for each sub-package is available at [pkg.go.dev][doc]:

* **client**: ovsdb client and API [![godoc for libovsdb/client][clientbadge]][clientdoc]
* **mapper**: mapping from tagged structs to ovsdb types [![godoc for libovsdb/mapper][mapperbadge]][mapperdoc]
Expand Down Expand Up @@ -209,15 +209,15 @@ It can be used as follows:
Package name (default "ovsmodel")

The result will be the definition of a Model per table defined in the ovsdb schema file.
Additionally, a function called `FullDatabaseModel()` that retuns the `DBModel` is created for convenience.
Additionally, a function called `FullDatabaseModel()` that returns the `DBModel` is created for convenience.

Example:

Download the schema:

ovsdb-client get-schema "tcp:localhost:6641" > mypackage/ovs-nb.ovsschema

Run `go gennerate`
Run `go generate`

cat <<EOF > mypackage/gen.go
package mypackage
Expand Down Expand Up @@ -263,7 +263,7 @@ Mac users can use [boot2docker](http://boot2docker.io)
# exit
docker-compose down

By invoking the command **make**, you will automatically get the same behaviour as what
By invoking the command **make**, you will automatically get the same behavior as what
is shown above. In other words, it will start the two containers and execute
**make test-local** from the test container.

Expand Down
24 changes: 12 additions & 12 deletions cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *RowCache) create(uuid string, m model.Model) error {
if reflect.TypeOf(m) != r.dataType {
return fmt.Errorf("expected data of type %s, but got %s", r.dataType.String(), reflect.TypeOf(m).String())
}
info, err := mapper.NewMapperInfo(&r.schema, m)
info, err := mapper.NewInfo(&r.schema, m)
if err != nil {
return err
}
Expand Down Expand Up @@ -146,11 +146,11 @@ func (r *RowCache) update(uuid string, m model.Model) error {
return fmt.Errorf("row %s does not exist", uuid)
}
oldRow := r.cache[uuid]
oldInfo, err := mapper.NewMapperInfo(&r.schema, oldRow)
oldInfo, err := mapper.NewInfo(&r.schema, oldRow)
if err != nil {
return err
}
newInfo, err := mapper.NewMapperInfo(&r.schema, m)
newInfo, err := mapper.NewInfo(&r.schema, m)
if err != nil {
return err
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func (r *RowCache) delete(uuid string) error {
return fmt.Errorf("row %s does not exist", uuid)
}
oldRow := r.cache[uuid]
oldInfo, err := mapper.NewMapperInfo(&r.schema, oldRow)
oldInfo, err := mapper.NewInfo(&r.schema, oldRow)
if err != nil {
return err
}
Expand Down Expand Up @@ -320,11 +320,11 @@ type TableCache struct {
dbModel *model.DBModel
}

// CacheData is the type for data that can be prepoulated in the cache
type CacheData map[string]map[string]model.Model
// Data is the type for data that can be prepoulated in the cache
type Data map[string]map[string]model.Model

// NewTableCache creates a new TableCache
func NewTableCache(schema *ovsdb.DatabaseSchema, dbModel *model.DBModel, data CacheData) (*TableCache, error) {
func NewTableCache(schema *ovsdb.DatabaseSchema, dbModel *model.DBModel, data Data) (*TableCache, error) {
if schema == nil || dbModel == nil {
return nil, fmt.Errorf("tablecache without databasemodel cannot be populated")
}
Expand Down Expand Up @@ -468,7 +468,7 @@ func (t *TableCache) Populate(tableUpdates ovsdb.TableUpdates) {
}
}

// AddEventHandler registers the supplied EventHandler to recieve cache events
// AddEventHandler registers the supplied EventHandler to receive cache events
func (t *TableCache) AddEventHandler(handler EventHandler) {
t.eventProcessor.AddEventHandler(handler)
}
Expand Down Expand Up @@ -565,7 +565,7 @@ func (e *eventProcessor) AddEvent(eventType string, table string, old model.Mode
// Run runs the eventProcessor loop.
// It will block until the stopCh has been closed
// Otherwise it will wait for events to arrive on the event channel
// Once recieved, it will dispatch the event to each registered handler
// Once received, it will dispatch the event to each registered handler
func (e *eventProcessor) Run(stopCh <-chan struct{}) {
for {
select {
Expand All @@ -588,7 +588,7 @@ func (e *eventProcessor) Run(stopCh <-chan struct{}) {
}
}

// createModel creates a new Model instance based on the Row information
// CreateModel creates a new Model instance based on the Row information
func (t *TableCache) CreateModel(tableName string, row *ovsdb.Row, uuid string) (model.Model, error) {
table := t.mapper.Schema.Table(tableName)
if table == nil {
Expand All @@ -605,7 +605,7 @@ func (t *TableCache) CreateModel(tableName string, row *ovsdb.Row, uuid string)
}

if uuid != "" {
mapperInfo, err := mapper.NewMapperInfo(table, model)
mapperInfo, err := mapper.NewInfo(table, model)
if err != nil {
return nil, err
}
Expand All @@ -617,7 +617,7 @@ func (t *TableCache) CreateModel(tableName string, row *ovsdb.Row, uuid string)
return model, nil
}

func hashColumnValues(info *mapper.MapperInfo, columns []string) (string, error) {
func hashColumnValues(info *mapper.Info, columns []string) (string, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
for _, column := range columns {
Expand Down
14 changes: 7 additions & 7 deletions cache/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func TestRowCacheCreate(t *testing.T) {
}
`), &schema)
require.Nil(t, err)
testData := CacheData{
testData := Data{
"Open_vSwitch": map[string]model.Model{"bar": &testModel{Foo: "bar"}},
}
tc, err := NewTableCache(&schema, db, testData)
Expand Down Expand Up @@ -173,7 +173,7 @@ func TestRowCacheCreateMultiIndex(t *testing.T) {
`), &schema)
require.Nil(t, err)
tSchema := schema.Table("Open_vSwitch")
testData := CacheData{
testData := Data{
"Open_vSwitch": map[string]model.Model{"bar": &testModel{Foo: "bar", Bar: "bar"}},
}
tc, err := NewTableCache(&schema, db, testData)
Expand Down Expand Up @@ -224,7 +224,7 @@ func TestRowCacheCreateMultiIndex(t *testing.T) {
assert.Error(t, err)
} else {
assert.Nil(t, err)
mapperInfo, err := mapper.NewMapperInfo(tSchema, tt.model)
mapperInfo, err := mapper.NewInfo(tSchema, tt.model)
require.Nil(t, err)
h, err := hashColumnValues(mapperInfo, []string{"foo", "bar"})
require.Nil(t, err)
Expand Down Expand Up @@ -256,7 +256,7 @@ func TestRowCacheUpdate(t *testing.T) {
}
`), &schema)
require.Nil(t, err)
testData := CacheData{
testData := Data{
"Open_vSwitch": map[string]model.Model{
"bar": &testModel{Foo: "bar"},
"foobar": &testModel{Foo: "foobar"},
Expand Down Expand Up @@ -328,7 +328,7 @@ func TestRowCacheUpdateMultiIndex(t *testing.T) {
`), &schema)
tSchema := schema.Table("Open_vSwitch")
require.Nil(t, err)
testData := CacheData{
testData := Data{
"Open_vSwitch": map[string]model.Model{
"bar": &testModel{Foo: "bar", Bar: "bar"},
"foobar": &testModel{Foo: "foobar", Bar: "foobar"},
Expand Down Expand Up @@ -377,7 +377,7 @@ func TestRowCacheUpdateMultiIndex(t *testing.T) {
assert.Error(t, err)
} else {
assert.Nil(t, err)
mapperInfo, err := mapper.NewMapperInfo(tSchema, tt.model)
mapperInfo, err := mapper.NewInfo(tSchema, tt.model)
require.Nil(t, err)
h, err := hashColumnValues(mapperInfo, []string{"foo", "bar"})
require.Nil(t, err)
Expand Down Expand Up @@ -409,7 +409,7 @@ func TestRowCacheDelete(t *testing.T) {
}
`), &schema)
require.Nil(t, err)
testData := CacheData{
testData := Data{
"Open_vSwitch": map[string]model.Model{
"bar": &testModel{Foo: "bar"},
},
Expand Down
20 changes: 10 additions & 10 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ type ConditionalAPI interface {
// the fields to be updated
Update(model.Model, ...interface{}) ([]ovsdb.Operation, error)

// Delete returns the Operations needed to delete the models seleted via the condition
// Delete returns the Operations needed to delete the models selected via the condition
Delete() ([]ovsdb.Operation, error)
}

Expand Down Expand Up @@ -224,17 +224,17 @@ func (a api) Get(m model.Model) error {
}

// If model contains _uuid value, we can access it via cache index
mapperInfo, err := mapper.NewMapperInfo(a.cache.Mapper().Schema.Table(table), m)
mapperInfo, err := mapper.NewInfo(a.cache.Mapper().Schema.Table(table), m)
if err != nil {
return err
}
if uuid, err := mapperInfo.FieldByColumn("_uuid"); err != nil && uuid != nil {
if found := tableCache.Row(uuid.(string)); found == nil {
found := tableCache.Row(uuid.(string))
if found == nil {
return ErrNotFound
} else {
reflect.ValueOf(m).Elem().Set(reflect.Indirect(reflect.ValueOf(found)))
return nil
}
reflect.ValueOf(m).Elem().Set(reflect.Indirect(reflect.ValueOf(found)))
return nil
}

// Look across the entire cache for table index equality
Expand All @@ -253,7 +253,7 @@ func (a api) Get(m model.Model) error {
}

// Create is a generic function capable of creating any row in the DB
// A valud Model (pointer to object) must be provided.
// A valid Model (pointer to object) must be provided.
func (a api) Create(models ...model.Model) ([]ovsdb.Operation, error) {
var operations []ovsdb.Operation

Expand All @@ -269,7 +269,7 @@ func (a api) Create(models ...model.Model) ([]ovsdb.Operation, error) {
table := a.cache.Mapper().Schema.Table(tableName)

// Read _uuid field, and use it as named-uuid
info, err := mapper.NewMapperInfo(table, model)
info, err := mapper.NewInfo(table, model)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -300,7 +300,7 @@ func (a api) Mutate(model model.Model, mutationObjs ...model.Mutation) ([]ovsdb.
var operations []ovsdb.Operation

if len(mutationObjs) < 1 {
return nil, fmt.Errorf("At least one Mutation must be provided")
return nil, fmt.Errorf("at least one Mutation must be provided")
}

tableName := a.cache.DBModel().FindTable(reflect.ValueOf(model).Type())
Expand All @@ -314,7 +314,7 @@ func (a api) Mutate(model model.Model, mutationObjs ...model.Mutation) ([]ovsdb.
return nil, err
}

info, err := mapper.NewMapperInfo(table, model)
info, err := mapper.NewInfo(table, model)
if err != nil {
return nil, err
}
Expand Down
16 changes: 8 additions & 8 deletions client/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func TestAPIListSimple(t *testing.T) {
for i := range lscacheList {
lscache[lscacheList[i].(*testLogicalSwitch).UUID] = lscacheList[i]
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch": lscache,
}
tcache := apiTestCache(t, testData)
Expand Down Expand Up @@ -149,7 +149,7 @@ func TestAPIListPredicate(t *testing.T) {
for i := range lscacheList {
lscache[lscacheList[i].(*testLogicalSwitch).UUID] = lscacheList[i]
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch": lscache,
}
tcache := apiTestCache(t, testData)
Expand Down Expand Up @@ -247,7 +247,7 @@ func TestAPIListFields(t *testing.T) {
for i := range lspcacheList {
lspcache[lspcacheList[i].(*testLogicalSwitchPort).UUID] = lspcacheList[i]
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch_Port": lspcache,
}
tcache := apiTestCache(t, testData)
Expand Down Expand Up @@ -444,7 +444,7 @@ func TestAPIGet(t *testing.T) {
for i := range lspCacheList {
lspCache[lspCacheList[i].(*testLogicalSwitchPort).UUID] = lspCacheList[i]
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch": lsCache,
"Logical_Switch_Port": lspCache,
}
Expand Down Expand Up @@ -526,7 +526,7 @@ func TestAPICreate(t *testing.T) {
for i := range lspCacheList {
lspCache[lspCacheList[i].(*testLogicalSwitchPort).UUID] = lspCacheList[i]
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch": lsCache,
"Logical_Switch_Port": lspCache,
}
Expand Down Expand Up @@ -642,7 +642,7 @@ func TestAPIMutate(t *testing.T) {
Tag: []int{1},
},
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch_Port": lspCache,
}
tcache := apiTestCache(t, testData)
Expand Down Expand Up @@ -812,7 +812,7 @@ func TestAPIUpdate(t *testing.T) {
Tag: []int{1},
},
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch_Port": lspCache,
}
tcache := apiTestCache(t, testData)
Expand Down Expand Up @@ -1060,7 +1060,7 @@ func TestAPIDelete(t *testing.T) {
Tag: []int{1},
},
}
testData := cache.CacheData{
testData := cache.Data{
"Logical_Switch_Port": lspCache,
}
tcache := apiTestCache(t, testData)
Expand Down
2 changes: 1 addition & 1 deletion client/api_test_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ type testLogicalSwitch struct {
Name string `ovsdb:"name"`
QosRules []string `ovsdb:"qos_rules"`
LoadBalancer []string `ovsdb:"load_balancer"`
DnsRecords []string `ovsdb:"dns_records"`
DNSRecords []string `ovsdb:"dns_records"`
OtherConfig map[string]string `ovsdb:"other_config"`
ForwardingGroups []string `ovsdb:"forwarding_groups"`
Acls []string `ovsdb:"acls"`
Expand Down
Loading

0 comments on commit e6caf40

Please sign in to comment.