diff --git a/util/anonymize/anonymize.go b/util/anonymize/anonymize.go index d5c13fb50..a4a41991f 100644 --- a/util/anonymize/anonymize.go +++ b/util/anonymize/anonymize.go @@ -6,8 +6,6 @@ import ( "math/rand" "unicode" - "github.com/anytypeio/go-anytype-middleware/core/block/editor/state" - "github.com/anytypeio/go-anytype-middleware/core/block/simple" "github.com/anytypeio/go-anytype-middleware/pb" "github.com/anytypeio/go-anytype-middleware/pkg/lib/bundle" "github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model" @@ -15,17 +13,6 @@ import ( "github.com/gogo/protobuf/types" ) -func State(s *state.State) (res *state.State) { - // blocks - res = s.Copy() - s.Iterate(func(b simple.Block) (isContinue bool) { - b.Model().Content = Block(b.Model()).Content - return true - }) - s.SetDetails(Struct(s.Details())) - return -} - func Change(ch *pb.Change) (res *pb.Change) { resB, _ := ch.Marshal() res = &pb.Change{} diff --git a/util/anyblocks/anyblocks.go b/util/anyblocks/anyblocks.go index bae120958..8f3ff4999 100644 --- a/util/anyblocks/anyblocks.go +++ b/util/anyblocks/anyblocks.go @@ -6,16 +6,6 @@ import ( "github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model" ) -func AllBlocksToCode(blocks []*model.Block) (blocksOut []*model.Block) { - for i, b := range blocks { - if t := b.GetText(); t != nil { - blocks[i].GetText().Style = model.BlockContentText_Code - } - } - - return PreprocessBlocks(blocks) -} - func PreprocessBlocks(blocks []*model.Block) (blocksOut []*model.Block) { blocksOut = []*model.Block{} diff --git a/util/console/console.go b/util/console/console.go index 5e32aa232..166666820 100644 --- a/util/console/console.go +++ b/util/console/console.go @@ -23,16 +23,6 @@ func Warn(format string, args ...interface{}) { aurora.Sprintf(aurora.BrightBlack(format), args...))) } -func Error(format string, args ...interface{}) { - fmt.Println(aurora.Sprintf(aurora.Yellow("> Error! %s"), - aurora.Sprintf(aurora.BrightBlack(format), args...))) -} - -func End(format string, args ...interface{}) { - Message(format, args...) - os.Exit(0) -} - func Fatal(format string, args ...interface{}) { fmt.Println(aurora.Sprintf(aurora.Red("> Fatal! %s"), aurora.Sprintf(aurora.BrightBlack(format), args...))) diff --git a/util/constans.go b/util/constans.go deleted file mode 100644 index 74f6a8424..000000000 --- a/util/constans.go +++ /dev/null @@ -1,3 +0,0 @@ -package util - -const SubIdSeparator = ":" diff --git a/util/ocache/ocache.go b/util/ocache/ocache.go index d2c8ffb52..628d444c0 100644 --- a/util/ocache/ocache.go +++ b/util/ocache/ocache.go @@ -31,12 +31,6 @@ type LoadFunc func(ctx context.Context, id string) (value Object, err error) type Option func(*oCache) -var WithLogServiceName = func(name string) Option { - return func(cache *oCache) { - cache.log = cache.log.With("service_name", name) - } -} - var WithTTL = func(ttl time.Duration) Option { return func(cache *oCache) { cache.ttl = ttl diff --git a/util/pbtypes/compare.go b/util/pbtypes/compare.go index 21607c18d..b445e3681 100644 --- a/util/pbtypes/compare.go +++ b/util/pbtypes/compare.go @@ -160,20 +160,6 @@ func RelationsDiff(rels1, rels2 []*model.Relation) (added []*model.Relation, upd return } -func RelationsEqual(rels1 []*model.Relation, rels2 []*model.Relation) (equal bool) { - if len(rels1) != len(rels2) { - return false - } - - for i := 0; i < len(rels2); i++ { - if !RelationEqual(rels1[i], rels2[i]) { - return false - } - } - - return true -} - func RelationEqualOmitDictionary(rel1 *model.Relation, rel2 *model.Relation) (equal bool) { if rel1 == nil && rel2 != nil { return false @@ -219,31 +205,6 @@ func RelationEqualOmitDictionary(rel1 *model.Relation, rel2 *model.Relation) (eq return true } -// RelationCompatible returns if provided relations are compatible in terms of underlying data format -// e.g. it is ok if relation can have a different name and selectDict, while having the same key and format -func RelationCompatible(rel1 *model.Relation, rel2 *model.Relation) (equal bool) { - if rel1 == nil && rel2 != nil { - return false - } - if rel2 == nil && rel1 != nil { - return false - } - if rel2 == nil && rel1 == nil { - return true - } - - if rel1.Key != rel2.Key { - return false - } - if rel1.Format != rel2.Format { - return false - } - - // todo: should we compare objectType here? - - return true -} - func RelationEqual(rel1 *model.Relation, rel2 *model.Relation) (equal bool) { if !RelationEqualOmitDictionary(rel1, rel2) { return false @@ -266,75 +227,6 @@ func RelationSelectDictEqual(dict1, dict2 []*model.RelationOption) bool { return true } -func RelationSelectDictDiff(dict1, dict2 []*model.RelationOption) (added []*model.RelationOption, updated []*model.RelationOption, removed []string) { - for i := 0; i < len(dict2); i++ { - if opt := GetOption(dict1, dict2[i].Id); opt == nil { - added = append(added, dict2[i]) - continue - } else { - if !OptionEqual(opt, dict2[i]) { - updated = append(updated, dict2[i]) - continue - } - } - } - - for i := 0; i < len(dict1); i++ { - if r := GetOption(dict2, dict1[i].Id); r == nil { - removed = append(removed, dict1[i].Id) - continue - } - } - return -} - -func RelationSelectDictDiffOmitScope(dict1, dict2 []*model.RelationOption) (added []*model.RelationOption, updated []*model.RelationOption, removed []string) { - for i := 0; i < len(dict2); i++ { - if opt := GetOption(dict1, dict2[i].Id); opt == nil { - added = append(added, dict2[i]) - continue - } else { - if !OptionEqualOmitScope(opt, dict2[i]) { - updated = append(updated, dict2[i]) - continue - } - } - } - - for i := 0; i < len(dict1); i++ { - if r := GetOption(dict2, dict1[i].Id); r == nil { - removed = append(removed, dict1[i].Id) - continue - } - } - return -} - -func OptionEqualOmitScope(opt1, opt2 *model.RelationOption) bool { - if (opt1 == nil) && (opt2 != nil) { - return false - } - - if (opt1 != nil) && (opt2 == nil) { - return false - } - - if opt1 == nil && opt2 == nil { - return true - } - - if opt1.Id != opt2.Id { - return false - } - if opt1.Text != opt2.Text { - return false - } - if opt1.Color != opt2.Color { - return false - } - return true -} - func OptionEqual(opt1, opt2 *model.RelationOption) bool { if (opt1 == nil) && (opt2 != nil) { return false @@ -496,18 +388,3 @@ func SortedRange(s *types.Struct, f func(k string, v *types.Value)) { f(k, s.Fields[k]) } } - -func RelationOptionsFilter(options []*model.RelationOption, f func(option *model.RelationOption) bool) []*model.RelationOption { - if len(options) == 0 { - return nil - } - - res := make([]*model.RelationOption, 0, len(options)) - for i := range options { - if f(options[i]) { - res = append(res, options[i]) - } - } - - return res -} diff --git a/util/pbtypes/copy.go b/util/pbtypes/copy.go index 9a80621dd..5b6b3cb78 100644 --- a/util/pbtypes/copy.go +++ b/util/pbtypes/copy.go @@ -29,23 +29,6 @@ func CopyBlock(in *model.Block) (out *model.Block) { return } -// CopyStructMap copies pb struct map, while reusing map values' pointers -func CopyStructMap(in *types.Struct) (out *types.Struct) { - if in == nil { - return nil - } - if in.Fields == nil { - return &types.Struct{} - } - - out = &types.Struct{Fields: make(map[string]*types.Value, len(in.Fields))} - for k, v := range in.Fields { - out.Fields[k] = v - } - - return -} - func CopyStruct(in *types.Struct) (out *types.Struct) { if in == nil { return nil @@ -82,13 +65,6 @@ func CopyVal(in *types.Value) (out *types.Value) { return } -func CopyRelationLink(in *model.RelationLink) (out *model.RelationLink) { - return &model.RelationLink{ - Key: in.Key, - Format: in.Format, - } -} - func CopyRelation(in *model.Relation) (out *model.Relation) { if in == nil { return nil @@ -154,28 +130,6 @@ func CopyRelations(in []*model.Relation) (out []*model.Relation) { return outWrapped.Relations } -func CopyOptions(in []*model.RelationOption) (out []*model.RelationOption) { - if in == nil { - return nil - } - - for _, inO := range in { - inCopy := *inO - out = append(out, &inCopy) - } - return -} - -func CopyRelationsToMap(in []*model.Relation) (out map[string]*model.Relation) { - out = make(map[string]*model.Relation, len(in)) - rels := CopyRelations(in) - for _, rel := range rels { - out[rel.Key] = rel - } - - return -} - func CopyFilter(in *model.BlockContentDataviewFilter) (out *model.BlockContentDataviewFilter) { buf := bytesPool.Get().([]byte) size := in.Size() @@ -189,28 +143,6 @@ func CopyFilter(in *model.BlockContentDataviewFilter) (out *model.BlockContentDa return } -func RelationsFilterKeys(in []*model.Relation, keys []string) (out []*model.Relation) { - for i, inRel := range in { - if slice.FindPos(keys, inRel.Key) >= 0 { - out = append(out, in[i]) - } - } - return -} - -func StructNotNilKeys(st *types.Struct) (keys []string) { - if st == nil || st.Fields == nil { - return nil - } - - for k, v := range st.Fields { - if v != nil { - keys = append(keys, k) - } - } - return -} - func EventsToSliceChange(changes []*pb.EventBlockDataviewSliceChange) []slice.Change { sliceOpMap := map[pb.EventBlockDataviewSliceOperation]slice.DiffOperation{ pb.EventBlockDataview_SliceOperationNone: slice.OperationNone, diff --git a/util/pbtypes/pbtypes.go b/util/pbtypes/pbtypes.go index 31b837dd0..3791522bc 100644 --- a/util/pbtypes/pbtypes.go +++ b/util/pbtypes/pbtypes.go @@ -238,39 +238,6 @@ func HasRelationLink(rels []*model.RelationLink, key string) bool { return false } -func MergeRelations(rels1 []*model.Relation, rels2 []*model.Relation) []*model.Relation { - if rels1 == nil { - return rels2 - } - if rels2 == nil { - return rels1 - } - - rels := make([]*model.Relation, 0, len(rels2)+len(rels1)) - for _, rel := range rels2 { - rels = append(rels, rel) - } - - for _, rel := range rels1 { - if HasRelation(rels, rel.Key) { - continue - } - rels = append(rels, rel) - } - - return rels -} - -func GetObjectType(ots []*model.ObjectType, url string) *model.ObjectType { - for i, ot := range ots { - if ot.Url == url { - return ots[i] - } - } - - return nil -} - func GetRelation(rels []*model.Relation, key string) *model.Relation { for i, rel := range rels { if rel.Key == key { @@ -291,16 +258,6 @@ func GetOption(opts []*model.RelationOption, id string) *model.RelationOption { return nil } -func HasOption(opts []*model.RelationOption, id string) bool { - for _, opt := range opts { - if opt.Id == id { - return true - } - } - - return false -} - func Get(st *types.Struct, keys ...string) *types.Value { for i, key := range keys { if st == nil || st.Fields == nil { @@ -315,15 +272,6 @@ func Get(st *types.Struct, keys ...string) *types.Value { return nil } -func GetRelationKeys(rels []*model.Relation) []string { - var keys []string - for _, rel := range rels { - keys = append(keys, rel.Key) - } - - return keys -} - func GetRelationListKeys(rels []*model.RelationLink) []string { var keys []string for _, rel := range rels { @@ -333,60 +281,6 @@ func GetRelationListKeys(rels []*model.RelationLink) []string { return keys } -func GetOptionIds(opts []*model.RelationOption) []string { - var keys []string - for _, opt := range opts { - keys = append(keys, opt.Id) - } - - return keys -} - -func MergeRelationsDicts(rels1 []*model.Relation, rels2 []*model.Relation) []*model.Relation { - rels := CopyRelations(rels1) - for _, rel2 := range rels2 { - var found bool - - for i, rel := range rels { - if rel.Key == rel2.Key { - rel2Copy := CopyRelation(rel2) - rels[i].SelectDict = rel2Copy.SelectDict - rels[i].Name = rel2Copy.Name - found = true - break - } - } - - if !found { - rels = append(rels, CopyRelation(rel2)) - } - } - return rels -} - -// MergeOptionsPreserveScope adds and updates options from opts2 into opts1 based on the ID -// in case opts2 doesn't have id that opts1 have it doesn't remove the existing one -// in case opts2 has the key that opts1 already have it updates everything except scope -func MergeOptionsPreserveScope(opts1 []*model.RelationOption, opts2 []*model.RelationOption) []*model.RelationOption { - opts := CopyOptions(opts1) - for _, opt2 := range opts2 { - var found bool - for i, opt := range opts { - if opt.Id == opt2.Id { - opts[i].Text = opt2.Text - opts[i].Color = opt2.Color - found = true - break - } - } - if !found { - opt2Copy := *opt2 - opts = append(opts, &opt2Copy) - } - } - return opts -} - // StructToMap converts a types.Struct to a map from strings to Go types. // StructToMap panics if s is invalid. func StructToMap(s *types.Struct) map[string]interface{} { @@ -400,59 +294,6 @@ func StructToMap(s *types.Struct) map[string]interface{} { return m } -func StructIsEmpty(s *types.Struct) bool { - return s == nil || len(s.Fields) == 0 -} - -func GetMapOfKeysAndValuesFromStruct(collection *types.Struct) map[string]*types.Value { - keyMap := map[string]*types.Value{} - if collection == nil { - return keyMap - } - keyStack := []string{""} - collStack := []*types.Struct{collection} - - for len(collStack) != 0 { - coll := collStack[len(collStack)-1] - lastKey := keyStack[len(keyStack)-1] - keyStack = keyStack[:len(keyStack)-1] - collStack = collStack[:len(collStack)-1] - for k, v := range coll.Fields { - subColl, ok := v.Kind.(*types.Value_StructValue) - updatedKey := lastKey - if updatedKey != "" { - updatedKey += "/" - } - updatedKey += k - if !ok { - keyMap[updatedKey] = v - continue - } - collStack = append(collStack, subColl.StructValue) - keyStack = append(keyStack, updatedKey) - } - } - return keyMap -} - -func CompareKeyMaps(before map[string]*types.Value, after map[string]*types.Value) (keysSet []string, keysRemoved []string) { - for k, afterValue := range after { - beforeValue, exists := before[k] - if exists && afterValue.Equal(beforeValue) { - continue - } - keysSet = append(keysSet, k) - } - - for k := range before { - if _, exists := after[k]; exists { - continue - } - keysRemoved = append(keysRemoved, k) - } - return -} - func ValueToInterface(v *types.Value) interface{} { switch k := v.Kind.(type) { case *types.Value_NullValue: @@ -476,18 +317,6 @@ func ValueToInterface(v *types.Value) interface{} { } } -func RelationFormatCanHaveListValue(format model.RelationFormat) bool { - switch format { - case model.RelationFormat_tag, - model.RelationFormat_file, - model.RelationFormat_object, - model.RelationFormat_number: - return true - default: - return false - } -} - func RelationIdToKey(id string) (string, error) { if strings.HasPrefix(id, addr.RelationKeyToIdPrefix) { return strings.TrimPrefix(id, addr.RelationKeyToIdPrefix), nil @@ -501,16 +330,6 @@ func RelationIdToKey(id string) (string, error) { return "", fmt.Errorf("incorrect id format") } -func Delete(st *types.Struct, key string) (ok bool) { - if st != nil && st.Fields != nil { - if _, ok := st.Fields[key]; ok { - delete(st.Fields, key) - return true - } - } - return false -} - type Getter interface { Get(key string) *types.Value } diff --git a/util/slice/slice.go b/util/slice/slice.go index 529f3e506..5d6531aad 100644 --- a/util/slice/slice.go +++ b/util/slice/slice.go @@ -144,8 +144,3 @@ func HasPrefix(value, prefix []string) bool { return true } -func Copy(s []string) []string { - res := make([]string, len(s)) - copy(res, s) - return res -} diff --git a/util/text/text.go b/util/text/text.go index 98404f2fc..b7ef7d5ac 100644 --- a/util/text/text.go +++ b/util/text/text.go @@ -1,9 +1,6 @@ package text import ( - "crypto/md5" - "fmt" - "strings" "unicode" "unicode/utf16" ) @@ -53,10 +50,4 @@ func StrToUTF16(str string) []uint16 { func UTF16ToStr(b []uint16) string { return string(utf16.Decode(b)) -} - -func SliceHash(keys []string) string { - s := strings.Join(keys, "_") - sum := md5.Sum([]byte(s)) - return fmt.Sprintf("%x", sum) -} +} \ No newline at end of file diff --git a/util/uri/uri.go b/util/uri/uri.go index d9739fb44..bf1453f76 100644 --- a/util/uri/uri.go +++ b/util/uri/uri.go @@ -5,8 +5,6 @@ import ( "os" "regexp" "strings" - - "github.com/anytypeio/go-anytype-middleware/pkg/lib/pb/model" ) var ( @@ -19,22 +17,6 @@ var ( winFilepathPrefixRegex = regexp.MustCompile(`^[a-zA-Z]:[\\\/]`) ) -func ValidateEmail(email string) bool { - if len(email) == 0 { - return false - } - - return noPrefixEmailRegexp.MatchString(email) -} - -func ValidatePhone(phone string) bool { - if len(phone) == 0 { - return false - } - - return noPrefixTelRegexp.MatchString(phone) -} - // ProcessURI tries to verify the web URI and return the normalized URI func ProcessURI(url string) (urlOut string, err error) { if len(url) == 0 { @@ -61,21 +43,3 @@ func ProcessURI(url string) (urlOut string, err error) { return url, fmt.Errorf("not a uri") } - -func ProcessAllURI(blocks []*model.Block) []*model.Block { - for bI, _ := range blocks { - if blocks[bI].GetText() != nil && blocks[bI].GetText().Marks != nil && len(blocks[bI].GetText().Marks.Marks) > 0 { - marks := blocks[bI].GetText().Marks.Marks - - for mI, _ := range marks { - if marks[mI].Type == model.BlockContentTextMark_Link { - marks[mI].Param, _ = ProcessURI(marks[mI].Param) - } - } - - blocks[bI].GetText().Marks.Marks = marks - } - } - - return blocks -}