diff options
Diffstat (limited to 'entity')
-rw-r--r-- | entity/dag/common_test.go | 2 | ||||
-rw-r--r-- | entity/dag/entity.go | 2 | ||||
-rw-r--r-- | entity/dag/entity_actions.go | 2 | ||||
-rw-r--r-- | entity/dag/example_test.go | 2 | ||||
-rw-r--r-- | entity/dag/op_noop.go | 2 | ||||
-rw-r--r-- | entity/dag/op_noop_test.go | 2 | ||||
-rw-r--r-- | entity/dag/op_set_metadata.go | 2 | ||||
-rw-r--r-- | entity/dag/op_set_metadata_test.go | 2 | ||||
-rw-r--r-- | entity/dag/operation.go | 2 | ||||
-rw-r--r-- | entity/dag/operation_pack.go | 2 | ||||
-rw-r--r-- | entity/dag/operation_pack_test.go | 2 | ||||
-rw-r--r-- | entity/dag/operation_testing.go | 2 | ||||
-rw-r--r-- | entity/id.go | 18 | ||||
-rw-r--r-- | entity/id_interleaved.go | 84 | ||||
-rw-r--r-- | entity/id_interleaved_test.go | 2 |
15 files changed, 94 insertions, 34 deletions
diff --git a/entity/dag/common_test.go b/entity/dag/common_test.go index df8622d4..713d0734 100644 --- a/entity/dag/common_test.go +++ b/entity/dag/common_test.go @@ -7,8 +7,8 @@ import ( "github.com/stretchr/testify/require" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" ) diff --git a/entity/dag/entity.go b/entity/dag/entity.go index 09f37246..f9b55bd4 100644 --- a/entity/dag/entity.go +++ b/entity/dag/entity.go @@ -9,8 +9,8 @@ import ( "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" "github.com/MichaelMure/git-bug/util/lamport" ) diff --git a/entity/dag/entity_actions.go b/entity/dag/entity_actions.go index 673799ec..c971f316 100644 --- a/entity/dag/entity_actions.go +++ b/entity/dag/entity_actions.go @@ -5,8 +5,8 @@ import ( "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" ) diff --git a/entity/dag/example_test.go b/entity/dag/example_test.go index 39d77f8d..9c6a4a62 100644 --- a/entity/dag/example_test.go +++ b/entity/dag/example_test.go @@ -6,9 +6,9 @@ import ( "os" "time" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" "github.com/MichaelMure/git-bug/entity/dag" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" ) diff --git a/entity/dag/op_noop.go b/entity/dag/op_noop.go index 2bc53578..c2d896a6 100644 --- a/entity/dag/op_noop.go +++ b/entity/dag/op_noop.go @@ -1,8 +1,8 @@ package dag import ( + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" ) var _ Operation = &NoOpOperation[Snapshot]{} diff --git a/entity/dag/op_noop_test.go b/entity/dag/op_noop_test.go index e2f5cde0..c7710b83 100644 --- a/entity/dag/op_noop_test.go +++ b/entity/dag/op_noop_test.go @@ -3,7 +3,7 @@ package dag import ( "testing" - "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/entities/identity" ) func TestNoopSerialize(t *testing.T) { diff --git a/entity/dag/op_set_metadata.go b/entity/dag/op_set_metadata.go index bf32171f..4d2d0f8c 100644 --- a/entity/dag/op_set_metadata.go +++ b/entity/dag/op_set_metadata.go @@ -5,8 +5,8 @@ import ( "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/util/text" ) diff --git a/entity/dag/op_set_metadata_test.go b/entity/dag/op_set_metadata_test.go index 4dab8a96..49603021 100644 --- a/entity/dag/op_set_metadata_test.go +++ b/entity/dag/op_set_metadata_test.go @@ -4,7 +4,7 @@ import ( "testing" "time" - "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/repository" "github.com/stretchr/testify/require" diff --git a/entity/dag/operation.go b/entity/dag/operation.go index 0227b3e0..1a778878 100644 --- a/entity/dag/operation.go +++ b/entity/dag/operation.go @@ -8,8 +8,8 @@ import ( "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" ) diff --git a/entity/dag/operation_pack.go b/entity/dag/operation_pack.go index b32a699f..08907623 100644 --- a/entity/dag/operation_pack.go +++ b/entity/dag/operation_pack.go @@ -10,8 +10,8 @@ import ( "github.com/ProtonMail/go-crypto/openpgp/packet" "github.com/pkg/errors" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" "github.com/MichaelMure/git-bug/util/lamport" ) diff --git a/entity/dag/operation_pack_test.go b/entity/dag/operation_pack_test.go index e438e4c5..bd8e8e03 100644 --- a/entity/dag/operation_pack_test.go +++ b/entity/dag/operation_pack_test.go @@ -6,7 +6,7 @@ import ( "github.com/stretchr/testify/require" - "github.com/MichaelMure/git-bug/identity" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/repository" ) diff --git a/entity/dag/operation_testing.go b/entity/dag/operation_testing.go index 29923eec..6ebdcae8 100644 --- a/entity/dag/operation_testing.go +++ b/entity/dag/operation_testing.go @@ -7,8 +7,8 @@ import ( "github.com/stretchr/testify/require" + "github.com/MichaelMure/git-bug/entities/identity" "github.com/MichaelMure/git-bug/entity" - "github.com/MichaelMure/git-bug/identity" "github.com/MichaelMure/git-bug/repository" ) diff --git a/entity/id.go b/entity/id.go index 8f3dc25b..49398da8 100644 --- a/entity/id.go +++ b/entity/id.go @@ -79,21 +79,3 @@ func (i Id) Validate() error { } return nil } - -/* - * Sorting - */ - -type Alphabetical []Id - -func (a Alphabetical) Len() int { - return len(a) -} - -func (a Alphabetical) Less(i, j int) bool { - return a[i] < a[j] -} - -func (a Alphabetical) Swap(i, j int) { - a[i], a[j] = a[j], a[i] -} diff --git a/entity/id_interleaved.go b/entity/id_interleaved.go index 0ce2ba00..28c59a42 100644 --- a/entity/id_interleaved.go +++ b/entity/id_interleaved.go @@ -1,13 +1,91 @@ package entity import ( + "fmt" + "io" "strings" + + "github.com/pkg/errors" ) +const UnsetCombinedId = CombinedId("unset") + +// CombinedId is an Id holding information from both a primary Id and a secondary Id. +// While it looks like a regular Id, do not just cast from one to another. +// Instead, use CombineIds and SeparateIds to create it and split it. +type CombinedId string + +// String return the identifier as a string +func (ci CombinedId) String() string { + return string(ci) +} + +// Human return the identifier, shortened for human consumption +func (ci CombinedId) Human() string { + format := fmt.Sprintf("%%.%ds", humanIdLength) + return fmt.Sprintf(format, ci) +} + +func (ci CombinedId) HasPrefix(prefix string) bool { + return strings.HasPrefix(string(ci), prefix) +} + +// UnmarshalGQL implement the Unmarshaler interface for gqlgen +func (ci *CombinedId) UnmarshalGQL(v interface{}) error { + _, ok := v.(string) + if !ok { + return fmt.Errorf("CombinedIds must be strings") + } + + *ci = v.(CombinedId) + + if err := ci.Validate(); err != nil { + return errors.Wrap(err, "invalid CombinedId") + } + + return nil +} + +// MarshalGQL implement the Marshaler interface for gqlgen +func (ci CombinedId) MarshalGQL(w io.Writer) { + _, _ = w.Write([]byte(`"` + ci.String() + `"`)) +} + +// Validate tell if the Id is valid +func (ci CombinedId) Validate() error { + // Special case to detect outdated repo + if len(ci) == 40 { + return fmt.Errorf("outdated repository format, please use https://github.com/MichaelMure/git-bug-migration to upgrade") + } + if len(ci) != idLength { + return fmt.Errorf("invalid length") + } + for _, r := range ci { + if (r < 'a' || r > 'z') && (r < '0' || r > '9') { + return fmt.Errorf("invalid character") + } + } + return nil +} + +// PrimaryPrefix is a helper to extract the primary prefix. +// If practical, use SeparateIds instead. +func (ci CombinedId) PrimaryPrefix() string { + primaryPrefix, _ := SeparateIds(string(ci)) + return primaryPrefix +} + +// SecondaryPrefix is a helper to extract the secondary prefix. +// If practical, use SeparateIds instead. +func (ci CombinedId) SecondaryPrefix() string { + _, secondaryPrefix := SeparateIds(string(ci)) + return secondaryPrefix +} + // CombineIds compute a merged Id holding information from both the primary Id // and the secondary Id. // -// This allow to later find efficiently a secondary element because we can access +// This allows to later find efficiently a secondary element because we can access // the primary one directly instead of searching for a primary that has a // secondary matching the Id. // @@ -32,7 +110,7 @@ import ( // 7: 4P, 3S // 10: 6P, 4S // 16: 11P, 5S -func CombineIds(primary Id, secondary Id) Id { +func CombineIds(primary Id, secondary Id) CombinedId { var id strings.Builder for i := 0; i < idLength; i++ { @@ -46,7 +124,7 @@ func CombineIds(primary Id, secondary Id) Id { } } - return Id(id.String()) + return CombinedId(id.String()) } // SeparateIds extract primary and secondary prefix from an arbitrary length prefix diff --git a/entity/id_interleaved_test.go b/entity/id_interleaved_test.go index ef9218c9..1dc79145 100644 --- a/entity/id_interleaved_test.go +++ b/entity/id_interleaved_test.go @@ -9,7 +9,7 @@ import ( func TestInterleaved(t *testing.T) { primary := Id("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWX______________") secondary := Id("YZ0123456789+/________________________________________________") - expectedId := Id("aYbZc0def1ghij2klmn3opqr4stuv5wxyz6ABCD7EFGH8IJKL9MNOP+QRST/UVWX") + expectedId := CombinedId("aYbZc0def1ghij2klmn3opqr4stuv5wxyz6ABCD7EFGH8IJKL9MNOP+QRST/UVWX") interleaved := CombineIds(primary, secondary) require.Equal(t, expectedId, interleaved) |