From 00eed77b451b8913771feef7a40067dd246d7872 Mon Sep 17 00:00:00 2001 From: Sida Chen Date: Tue, 19 Feb 2019 16:33:27 -0500 Subject: [PATCH] database: Add feature_type database model --- database/affected_feature_type.go | 26 -------------- database/feature_type.go | 52 ++++++++++++++++++++++++++++ database/pgsql/feature_type.go | 53 +++++++++++++++++++++++++++++ database/pgsql/feature_type_test.go | 38 +++++++++++++++++++++ 4 files changed, 143 insertions(+), 26 deletions(-) delete mode 100644 database/affected_feature_type.go create mode 100644 database/feature_type.go create mode 100644 database/pgsql/feature_type.go create mode 100644 database/pgsql/feature_type_test.go diff --git a/database/affected_feature_type.go b/database/affected_feature_type.go deleted file mode 100644 index 950ddeae..00000000 --- a/database/affected_feature_type.go +++ /dev/null @@ -1,26 +0,0 @@ -// Copyright 2018 clair authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package database - -// AffectedFeatureType indicates the type of feature that a vulnerability -// affects. -type AffectedFeatureType string - -const ( - // AffectSourcePackage indicates the vulnerability affects a source package. - AffectSourcePackage AffectedFeatureType = "source" - // AffectBinaryPackage indicates the vulnerability affects a binary package. - AffectBinaryPackage AffectedFeatureType = "binary" -) diff --git a/database/feature_type.go b/database/feature_type.go new file mode 100644 index 00000000..dea59fb2 --- /dev/null +++ b/database/feature_type.go @@ -0,0 +1,52 @@ +// Copyright 2019 clair authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package database + +import ( + "database/sql/driver" + "fmt" +) + +// FeatureType indicates the type of feature that a vulnerability +// affects. +type FeatureType string + +const ( + SourcePackage FeatureType = "source" + BinaryPackage FeatureType = "binary" +) + +var featureTypes = []FeatureType{ + SourcePackage, + BinaryPackage, +} + +// Scan implements the database/sql.Scanner interface. +func (t *FeatureType) Scan(value interface{}) error { + val := value.(string) + for _, ft := range featureTypes { + if string(ft) == val { + *t = ft + return nil + } + } + + panic(fmt.Sprintf("invalid feature type received from database: '%s'", val)) +} + +// Value implements the database/sql/driver.Valuer interface. +func (t *FeatureType) Value() (driver.Value, error) { + return string(*t), nil +} diff --git a/database/pgsql/feature_type.go b/database/pgsql/feature_type.go new file mode 100644 index 00000000..bccf0cd8 --- /dev/null +++ b/database/pgsql/feature_type.go @@ -0,0 +1,53 @@ +// Copyright 2019 clair authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pgsql + +import "github.com/coreos/clair/database" + +const ( + selectAllFeatureTypes = `SELECT id, name FROM feature_type` +) + +type featureTypes struct { + byID map[int]database.FeatureType + byName map[database.FeatureType]int +} + +func newFeatureTypes() *featureTypes { + return &featureTypes{make(map[int]database.FeatureType), make(map[database.FeatureType]int)} +} + +func (tx *pgSession) getFeatureTypeMap() (*featureTypes, error) { + rows, err := tx.Query(selectAllFeatureTypes) + if err != nil { + return nil, err + } + + types := newFeatureTypes() + for rows.Next() { + var ( + id int + name database.FeatureType + ) + if err := rows.Scan(&id, &name); err != nil { + return nil, err + } + + types.byID[id] = name + types.byName[name] = id + } + + return types, nil +} diff --git a/database/pgsql/feature_type_test.go b/database/pgsql/feature_type_test.go new file mode 100644 index 00000000..f8cbf732 --- /dev/null +++ b/database/pgsql/feature_type_test.go @@ -0,0 +1,38 @@ +// Copyright 2019 clair authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pgsql + +import ( + "testing" + + "github.com/stretchr/testify/require" + + "github.com/coreos/clair/database" +) + +func TestGetFeatureTypeMap(t *testing.T) { + tx, cleanup := createTestPgSession(t, "TestGetFeatureTypeMap") + defer cleanup() + + types, err := tx.getFeatureTypeMap() + if err != nil { + require.Nil(t, err, err.Error()) + } + + require.Equal(t, database.SourcePackage, types.byID[1]) + require.Equal(t, database.BinaryPackage, types.byID[2]) + require.Equal(t, 1, types.byName[database.SourcePackage]) + require.Equal(t, 2, types.byName[database.BinaryPackage]) +}