github.com/ipld/go-ipld-prime@v0.21.0/datamodel/link.go (about) 1 package datamodel 2 3 // Link is a special kind of value in IPLD which can be "loaded" to access more nodes. 4 // 5 // Nodes can be a Link: "link" is one of the kinds in the IPLD Data Model; 6 // and accordingly there is an `ipld.Kind_Link` enum value, and Node has an `AsLink` method. 7 // 8 // Links are considered a scalar value in the IPLD Data Model, 9 // but when "loaded", the result can be any other IPLD kind: 10 // maps, lists, strings, etc. 11 // 12 // Link is an interface in the go-ipld implementation, 13 // but the most common instantiation of it comes from the `linking/cid` package, 14 // and represents CIDs (see https://github.com/multiformats/cid). 15 // 16 // The Link interface says very little by itself; it's generally necessary to 17 // use type assertions to unpack more specific forms of data. 18 // The only real contract is that the Link must be able to return a LinkPrototype, 19 // which must be able to produce new Link values of a similar form. 20 // (In practice: if you're familiar with CIDs: Link.Prototype is analogous to cid.Prefix.) 21 // 22 // The traversal package contains powerful features for walking through large graphs of Nodes 23 // while automatically loading and traversing links as the walk goes. 24 // 25 // Note that the Link interface should typically be inhabited by a struct or string, as opposed to a pointer. 26 // This is because Link is often desirable to be able to use as a golang map key, 27 // and in that context, pointers would not result in the desired behavior. 28 type Link interface { 29 // Prototype should return a LinkPrototype which carries the information 30 // to make more Link values similar to this one (but with different hashes). 31 Prototype() LinkPrototype 32 33 // String should return a reasonably human-readable debug-friendly representation the Link. 34 // There is no contract that requires that the string be able to be parsed back into a Link value, 35 // but the string should be unique (e.g. not elide any parts of the hash). 36 String() string 37 38 // Binary should return the densest possible encoding of the Link. 39 // The value need not be printable or human-readable; 40 // the golang string type is used for immutability and for ease of use as a map key. 41 // As with the String method, the returned value may not elide any parts of the hash. 42 // 43 // Note that there is still no contract that the returned value should be parsable back into a Link value; 44 // not even in the case of `lnk.Prototype().BuildLink(lnk.Binary()[:])`. 45 // This is because the value returned by this method may contain data that the LinkPrototype would also restate. 46 // (For a concrete example: if using CIDs, this method will return a binary string that includes 47 // the cid version indicator, the multicodec and multihash indicators, etc, in addition to the hash itself -- 48 // whereas the LinkPrototype.BuildLink function still expects to receive only the hash itself alone.) 49 Binary() string 50 } 51 52 // LinkPrototype encapsulates any implementation details and parameters 53 // necessary for creating a Link, expect for the hash result itself. 54 // 55 // LinkPrototype, like Link, is an interface in go-ipld, 56 // but the most common instantiation of it comes from the `linking/cid` package, 57 // and represents CIDs (see https://github.com/multiformats/cid). 58 // If using CIDs as an implementation, LinkPrototype will encapsulate information 59 // like multihashType, multicodecType, and cidVersion, for example. 60 // (LinkPrototype is analogous to cid.Prefix.) 61 type LinkPrototype interface { 62 // BuildLink should return a new Link value based on the given hashsum. 63 // The hashsum argument should typically be a value returned from a 64 // https://golang.org/pkg/hash/#Hash.Sum call. 65 // 66 // The hashsum reference must not be retained (the caller is free to reuse it). 67 BuildLink(hashsum []byte) Link 68 }