fork of indigo with slightly nicer lexgen

data: blob extraction helper

Changed files
+62 -3
atproto
+28
atproto/data/basic_test.go
··· 6 6 7 7 "github.com/bluesky-social/indigo/atproto/syntax" 8 8 9 + "github.com/ipfs/go-cid" 9 10 "github.com/stretchr/testify/assert" 10 11 ) 11 12 ··· 57 58 _, err = json.Marshal(obj) 58 59 assert.NoError(err) 59 60 } 61 + 62 + func TestExtractBlobs(t *testing.T) { 63 + assert := assert.New(t) 64 + 65 + cid1, _ := cid.Parse("bafkreiccldh766hwcnuxnf2wh6jgzepf2nlu2lvcllt63eww5p6chi4ity") 66 + obj := map[string]interface{}{ 67 + "a": 5, 68 + "b": 123, 69 + "c": map[string]interface{}{ 70 + "blb": Blob{ 71 + Size: 567, 72 + MimeType: "image/jpeg", 73 + Ref: CIDLink(cid1), 74 + }, 75 + }, 76 + "d": []interface{}{ 77 + 123, 78 + Blob{ 79 + Size: 123, 80 + MimeType: "image/png", 81 + Ref: CIDLink(cid1), 82 + }, 83 + }, 84 + } 85 + blbs := ExtractBlobs(obj) 86 + assert.Equal(2, len(blbs)) 87 + }
+31
atproto/data/data.go
··· 45 45 return out, nil 46 46 } 47 47 48 + // Recursively finds all the "blob" objects from generic atproto data (which has already been parsed). 49 + // 50 + // Returns an array with all Blob instances; does not de-dupe. 51 + func ExtractBlobs(obj map[string]any) []Blob { 52 + return extractBlobsAtom(obj) 53 + } 54 + 55 + func extractBlobsAtom(atom any) []Blob { 56 + out := []Blob{} 57 + switch v := atom.(type) { 58 + case Blob: 59 + out = append(out, v) 60 + case []any: 61 + for _, el := range v { 62 + down := extractBlobsAtom(el) 63 + for _, d := range down { 64 + out = append(out, d) 65 + } 66 + } 67 + case map[string]any: 68 + for _, val := range v { 69 + down := extractBlobsAtom(val) 70 + for _, d := range down { 71 + out = append(out, d) 72 + } 73 + } 74 + default: 75 + } 76 + return out 77 + } 78 + 48 79 // Serializes generic atproto data (object) to DAG-CBOR bytes 49 80 // 50 81 // Does not re-validate that data conforms to atproto data model, but does handle Blob, Bytes, and CIDLink as expected.
+3 -3
atproto/data/extract_test.go
··· 1 1 package data 2 2 3 3 import ( 4 - "testing" 5 - "os" 6 4 "io" 5 + "os" 6 + "testing" 7 7 8 8 "github.com/stretchr/testify/assert" 9 9 ) ··· 30 30 if err != nil { 31 31 t.Fail() 32 32 } 33 - cborBytes, err := io.ReadAll(inFile) 33 + cborBytes, err := io.ReadAll(inFile) 34 34 if err != nil { 35 35 t.Fail() 36 36 }