| 1 |
package activitypub |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"reflect" |
| 6 |
"strings" |
| 7 |
) |
| 8 |
|
| 9 |
|
| 10 |
type Item = ObjectOrLink |
| 11 |
|
| 12 |
const ( |
| 13 |
|
| 14 |
EmptyIRI IRI = "" |
| 15 |
|
| 16 |
|
| 17 |
NilIRI IRI = "-" |
| 18 |
|
| 19 |
|
| 20 |
EmptyID = EmptyIRI |
| 21 |
|
| 22 |
NilID = NilIRI |
| 23 |
) |
| 24 |
|
| 25 |
func itemsNeedSwapping(i1, i2 Item) bool { |
| 26 |
if IsIRI(i1) && !IsIRI(i2) { |
| 27 |
return true |
| 28 |
} |
| 29 |
t1 := i1.GetType() |
| 30 |
t2 := i2.GetType() |
| 31 |
if ObjectTypes.Match(t2) { |
| 32 |
return !ObjectTypes.Match(t1) |
| 33 |
} |
| 34 |
return false |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
func ItemsEqual(it, with Item) bool { |
| 39 |
if IsNil(it) || IsNil(with) { |
| 40 |
return IsNil(with) && IsNil(it) |
| 41 |
} |
| 42 |
if itemsNeedSwapping(it, with) { |
| 43 |
return ItemsEqual(with, it) |
| 44 |
} |
| 45 |
result := false |
| 46 |
if IsIRI(with) || IsIRI(it) { |
| 47 |
|
| 48 |
|
| 49 |
result = it.GetLink().Equals(with.GetLink(), false) |
| 50 |
} else if IsItemCollection(it) { |
| 51 |
if !IsItemCollection(with) { |
| 52 |
return false |
| 53 |
} |
| 54 |
_ = OnItemCollection(it, func(c *ItemCollection) error { |
| 55 |
result = c.Equals(with) |
| 56 |
return nil |
| 57 |
}) |
| 58 |
} else if IsLink(it) { |
| 59 |
_ = OnLink(it, func(l *Link) error { |
| 60 |
result = l.Equals(with) |
| 61 |
return nil |
| 62 |
}) |
| 63 |
} else if IsObject(it) { |
| 64 |
_ = OnObject(it, func(i *Object) error { |
| 65 |
result = i.Equals(with) |
| 66 |
return nil |
| 67 |
}) |
| 68 |
if ActivityTypes.Match(with.GetType()) { |
| 69 |
_ = OnActivity(it, func(i *Activity) error { |
| 70 |
result = i.Equals(with) |
| 71 |
return nil |
| 72 |
}) |
| 73 |
} else if ActorTypes.Match(with.GetType()) { |
| 74 |
_ = OnActor(it, func(i *Actor) error { |
| 75 |
result = i.Equals(with) |
| 76 |
return nil |
| 77 |
}) |
| 78 |
} else if it.IsCollection() { |
| 79 |
if CollectionType.Match(it.GetType()) { |
| 80 |
_ = OnCollection(it, func(c *Collection) error { |
| 81 |
result = c.Equals(with) |
| 82 |
return nil |
| 83 |
}) |
| 84 |
} |
| 85 |
if OrderedCollectionType.Match(it.GetType()) { |
| 86 |
_ = OnOrderedCollection(it, func(c *OrderedCollection) error { |
| 87 |
result = c.Equals(with) |
| 88 |
return nil |
| 89 |
}) |
| 90 |
} |
| 91 |
if CollectionPageType.Match(it.GetType()) { |
| 92 |
_ = OnCollectionPage(it, func(c *CollectionPage) error { |
| 93 |
result = c.Equals(with) |
| 94 |
return nil |
| 95 |
}) |
| 96 |
} |
| 97 |
if OrderedCollectionPageType.Match(it.GetType()) { |
| 98 |
_ = OnOrderedCollectionPage(it, func(c *OrderedCollectionPage) error { |
| 99 |
result = c.Equals(with) |
| 100 |
return nil |
| 101 |
}) |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
return result |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
func IsItemCollection(it LinkOrIRI) bool { |
| 110 |
if _, ok := it.(ItemCollection); ok { |
| 111 |
return ok |
| 112 |
} |
| 113 |
if _, ok := it.(*ItemCollection); ok { |
| 114 |
return ok |
| 115 |
} |
| 116 |
return IsIRIs(it) |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
func IsIRI(it LinkOrIRI) bool { |
| 121 |
if it == nil { |
| 122 |
return false |
| 123 |
} |
| 124 |
if _, ok := it.(IRI); ok { |
| 125 |
return true |
| 126 |
} |
| 127 |
if iri, ok := it.(*IRI); ok { |
| 128 |
return iri != nil |
| 129 |
} |
| 130 |
return false |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
func IsIRIs(it LinkOrIRI) bool { |
| 135 |
if it == nil { |
| 136 |
return false |
| 137 |
} |
| 138 |
if iris, ok := it.(IRIs); ok { |
| 139 |
return iris != nil |
| 140 |
} |
| 141 |
if iris, ok := it.(*IRIs); ok { |
| 142 |
return iris != nil |
| 143 |
} |
| 144 |
return false |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func IsLink(it LinkOrIRI) bool { |
| 149 |
if it == nil { |
| 150 |
return false |
| 151 |
} |
| 152 |
if _, ok := it.(Link); ok { |
| 153 |
return true |
| 154 |
} |
| 155 |
if l, ok := it.(*Link); ok { |
| 156 |
return l != nil |
| 157 |
} |
| 158 |
return false |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
func IsObject(it LinkOrIRI) bool { |
| 163 |
if it == nil { |
| 164 |
return false |
| 165 |
} |
| 166 |
switch ob := it.(type) { |
| 167 |
case Object: |
| 168 |
return true |
| 169 |
case *Object: |
| 170 |
return ob != nil |
| 171 |
case Actor: |
| 172 |
return true |
| 173 |
case *Actor: |
| 174 |
return ob != nil |
| 175 |
case Profile: |
| 176 |
return true |
| 177 |
case *Profile: |
| 178 |
return ob != nil |
| 179 |
case Place: |
| 180 |
return true |
| 181 |
case *Place: |
| 182 |
return ob != nil |
| 183 |
case Relationship: |
| 184 |
return true |
| 185 |
case *Relationship: |
| 186 |
return ob != nil |
| 187 |
case Tombstone: |
| 188 |
return true |
| 189 |
case *Tombstone: |
| 190 |
return ob != nil |
| 191 |
case Activity: |
| 192 |
return true |
| 193 |
case *Activity: |
| 194 |
return ob != nil |
| 195 |
case IntransitiveActivity: |
| 196 |
return true |
| 197 |
case *IntransitiveActivity: |
| 198 |
return ob != nil |
| 199 |
case Question: |
| 200 |
return true |
| 201 |
case *Question: |
| 202 |
return ob != nil |
| 203 |
case Collection: |
| 204 |
return true |
| 205 |
case *Collection: |
| 206 |
return ob != nil |
| 207 |
case CollectionPage: |
| 208 |
return true |
| 209 |
case *CollectionPage: |
| 210 |
return ob != nil |
| 211 |
case OrderedCollection: |
| 212 |
return true |
| 213 |
case *OrderedCollection: |
| 214 |
return ob != nil |
| 215 |
case OrderedCollectionPage: |
| 216 |
return true |
| 217 |
case *OrderedCollectionPage: |
| 218 |
return ob != nil |
| 219 |
default: |
| 220 |
return false |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
func IsNil(it LinkOrIRI) bool { |
| 226 |
if it == nil { |
| 227 |
return true |
| 228 |
} |
| 229 |
|
| 230 |
isNil := false |
| 231 |
if IsIRI(it) { |
| 232 |
var l IRI |
| 233 |
if lp, ok := it.(*IRI); ok { |
| 234 |
l = *lp |
| 235 |
} else { |
| 236 |
l, _ = it.(IRI) |
| 237 |
} |
| 238 |
isNil = len(l) == 0 || strings.EqualFold(l.String(), NilIRI.String()) |
| 239 |
} else if IsItemCollection(it) { |
| 240 |
if v, ok := it.(ItemCollection); ok { |
| 241 |
return v == nil |
| 242 |
} |
| 243 |
if v, ok := it.(*ItemCollection); ok { |
| 244 |
return v == nil |
| 245 |
} |
| 246 |
if v, ok := it.(IRIs); ok { |
| 247 |
return v == nil |
| 248 |
} |
| 249 |
if v, ok := it.(*IRIs); ok { |
| 250 |
return v == nil |
| 251 |
} |
| 252 |
} else if IsObject(it) { |
| 253 |
if ob, ok := it.(Item); ok { |
| 254 |
_ = OnObject(ob, func(o *Object) error { |
| 255 |
isNil = o == nil |
| 256 |
return nil |
| 257 |
}) |
| 258 |
} |
| 259 |
} else if IsLink(it) { |
| 260 |
_ = OnLink(it, func(l *Link) error { |
| 261 |
isNil = l == nil |
| 262 |
return nil |
| 263 |
}) |
| 264 |
} else { |
| 265 |
|
| 266 |
|
| 267 |
v := reflect.ValueOf(it) |
| 268 |
isNil = v.Kind() == reflect.Pointer && v.IsNil() |
| 269 |
} |
| 270 |
return isNil |
| 271 |
} |
| 272 |
|
| 273 |
func ErrorInvalidType[T Objects | Links](received LinkOrIRI) error { |
| 274 |
return fmt.Errorf("unable to convert %T to %T", received, new(T)) |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
func OnItem(it Item, fn func(Item) error) error { |
| 283 |
if it == nil { |
| 284 |
return nil |
| 285 |
} |
| 286 |
if !IsItemCollection(it) { |
| 287 |
return fn(it) |
| 288 |
} |
| 289 |
return OnItemCollection(it, func(col *ItemCollection) error { |
| 290 |
for _, it := range *col { |
| 291 |
if err := OnItem(it, fn); err != nil { |
| 292 |
return err |
| 293 |
} |
| 294 |
} |
| 295 |
return nil |
| 296 |
}) |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
func OnItemCollection(it LinkOrIRI, fn WithItemCollectionFn) error { |
| 304 |
if it == nil { |
| 305 |
return nil |
| 306 |
} |
| 307 |
col, err := ToItemCollection(it) |
| 308 |
if err != nil { |
| 309 |
return err |
| 310 |
} |
| 311 |
return fn(col) |
| 312 |
} |