aboutsummaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorPaul Duncan <pabs@pablotron.org>2022-02-01 01:34:16 -0500
committerPaul Duncan <pabs@pablotron.org>2022-02-01 01:34:16 -0500
commit58d86c1dcbc27772bf9eb91817091e6b430b90e8 (patch)
treed9874a08ffef125a6392433b20d1551226d5b2a9 /internal
parentb21b93ddda2f94d38c62d2c8aacc3b4b193c7742 (diff)
downloadcvez-58d86c1dcbc27772bf9eb91817091e6b430b90e8.tar.bz2
cvez-58d86c1dcbc27772bf9eb91817091e6b430b90e8.zip
add internal/feed/dataversion*go
Diffstat (limited to 'internal')
-rw-r--r--internal/feed/dataversion.go36
-rw-r--r--internal/feed/dataversion_string.go23
-rw-r--r--internal/feed/dataversion_test.go65
3 files changed, 124 insertions, 0 deletions
diff --git a/internal/feed/dataversion.go b/internal/feed/dataversion.go
new file mode 100644
index 0000000..c6f1b8d
--- /dev/null
+++ b/internal/feed/dataversion.go
@@ -0,0 +1,36 @@
+package feed
+
+//go:generate stringer -linecomment -type=DataVersion
+
+import (
+ "encoding/json"
+ "fmt"
+)
+
+// Data version for NVD feeds and feed items.
+type DataVersion byte
+
+const (
+ V40 DataVersion = iota // 4.0
+)
+
+// Unmarshal data version from JSON.
+func (me *DataVersion) UnmarshalJSON(b []byte) error {
+ // decode string, check for error
+ var s string
+ if err := json.Unmarshal(b, &s); err != nil {
+ return err
+ }
+
+ // check value
+ switch s {
+ case "4.0":
+ *me = V40
+ default:
+ // return error
+ return fmt.Errorf("unknown data version: %s", s)
+ }
+
+ // return success
+ return nil
+}
diff --git a/internal/feed/dataversion_string.go b/internal/feed/dataversion_string.go
new file mode 100644
index 0000000..26a0fdb
--- /dev/null
+++ b/internal/feed/dataversion_string.go
@@ -0,0 +1,23 @@
+// Code generated by "stringer -linecomment -type=DataVersion"; DO NOT EDIT.
+
+package feed
+
+import "strconv"
+
+func _() {
+ // An "invalid array index" compiler error signifies that the constant values have changed.
+ // Re-run the stringer command to generate them again.
+ var x [1]struct{}
+ _ = x[V40-0]
+}
+
+const _DataVersion_name = "4.0"
+
+var _DataVersion_index = [...]uint8{0, 3}
+
+func (i DataVersion) String() string {
+ if i >= DataVersion(len(_DataVersion_index)-1) {
+ return "DataVersion(" + strconv.FormatInt(int64(i), 10) + ")"
+ }
+ return _DataVersion_name[_DataVersion_index[i]:_DataVersion_index[i+1]]
+}
diff --git a/internal/feed/dataversion_test.go b/internal/feed/dataversion_test.go
new file mode 100644
index 0000000..6bf683c
--- /dev/null
+++ b/internal/feed/dataversion_test.go
@@ -0,0 +1,65 @@
+package feed
+
+import (
+ "encoding/json"
+ "testing"
+)
+
+func TestDataVersionUnmarshalInvalidData(t *testing.T) {
+ test := []byte(`{}`)
+ var val DataVersion
+
+ if err := json.Unmarshal(test, &val); err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ }
+}
+
+func TestDataVersionUnmarshalUnknown(t *testing.T) {
+ test := []byte(`"foo"`)
+ exp := "unknown data version: foo"
+ var val DataVersion
+
+ err := json.Unmarshal(test, &val)
+ if err == nil {
+ t.Errorf("got \"%s\", exp error", val)
+ return
+ }
+
+ if err.Error() != exp {
+ t.Errorf("got \"%s\", exp \"%s\"", err.Error(), exp)
+ }
+}
+
+func TestDataVersionUnmarshalValid(t *testing.T) {
+ test := []byte(`"4.0"`)
+ exp := V40
+ var got DataVersion
+
+ if err := json.Unmarshal(test, &got); err != nil {
+ t.Error(err)
+ return
+ }
+
+ if got != exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, exp)
+ }
+}
+
+func TestDataVersionString(t *testing.T) {
+ tests := []struct {
+ val DataVersion
+ exp string
+ } {
+ { V40, "4.0" },
+ { DataVersion(255), "DataVersion(255)" },
+ }
+
+ for _, test := range(tests) {
+ t.Run(test.val.String(), func(t *testing.T) {
+ got := test.val.String()
+ if got != test.exp {
+ t.Errorf("got \"%s\", exp \"%s\"", got, test.exp)
+ }
+ })
+ }
+}