From 82029fe0bdcb413ec2a92f7e15b8baeb9d81e4c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bart=C5=82omiej=20=C5=9Awi=C4=99cki?= Date: Sat, 1 Nov 2025 22:37:43 +0100 Subject: [PATCH] Move blobtypes to common --- blobtypes/errors.go | 24 +++++++++++++++++++++++ blobtypes/list.go | 43 ++++++++++++++++++++++++++++++++++++++++++ blobtypes/list_test.go | 36 +++++++++++++++++++++++++++++++++++ 3 files changed, 103 insertions(+) create mode 100644 blobtypes/errors.go create mode 100644 blobtypes/list.go create mode 100644 blobtypes/list_test.go diff --git a/blobtypes/errors.go b/blobtypes/errors.go new file mode 100644 index 0000000..6dfd5ba --- /dev/null +++ b/blobtypes/errors.go @@ -0,0 +1,24 @@ +/* +Copyright © 2025 Bartłomiej Święcki (byo) + +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 blobtypes + +import "errors" + +var ( + ErrUnknownBlobType = errors.New("unknown blob type") + ErrValidationFailed = errors.New("blob validation failed") +) diff --git a/blobtypes/list.go b/blobtypes/list.go new file mode 100644 index 0000000..2cb03a8 --- /dev/null +++ b/blobtypes/list.go @@ -0,0 +1,43 @@ +/* +Copyright © 2025 Bartłomiej Święcki (byo) + +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 blobtypes + +import ( + "fmt" + + "github.com/cinode/go-common/blob" +) + +var ( + Invalid = blob.NewType(0x00) + Static = blob.NewType(0x01) + DynamicLink = blob.NewType(0x02) +) + +var All = map[string]blob.Type{ + "Static": Static, + "DynamicLink": DynamicLink, +} + +func ToName(t blob.Type) string { + for name, tp := range All { + if tp == t { + return name + } + } + return fmt.Sprintf("Invalid(%d)", t.IDByte()) +} diff --git a/blobtypes/list_test.go b/blobtypes/list_test.go new file mode 100644 index 0000000..823afcd --- /dev/null +++ b/blobtypes/list_test.go @@ -0,0 +1,36 @@ +/* +Copyright © 2025 Bartłomiej Święcki (byo) + +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 blobtypes + +import ( + "testing" + + "github.com/cinode/go-common/blob" + "github.com/cinode/go-common/picotestify/require" +) + +func TestToName(t *testing.T) { + t.Run("valid known types", func(t *testing.T) { + for name, bType := range All { + require.Equal(t, name, ToName(bType)) + } + }) + t.Run("invalid type", func(t *testing.T) { + require.Equal(t, "Invalid(0)", ToName(Invalid)) + require.Equal(t, "Invalid(255)", ToName(blob.NewType(255))) + }) +}