BuildRelationPathChain builds the complete relation path chain for multi-hop nested attributes
(sourceEntityType, targetEntityType string)
| 435 | |
| 436 | // BuildRelationPathChain builds the complete relation path chain for multi-hop nested attributes |
| 437 | func (g *LinkedSchemaGraph) BuildRelationPathChain(sourceEntityType, targetEntityType string) ([]*base.RelationReference, error) { |
| 438 | // Try direct relation first |
| 439 | relationName, err := g.GetInverseRelation(sourceEntityType, targetEntityType) |
| 440 | if err == nil { |
| 441 | // Direct relation exists, return single hop |
| 442 | return []*base.RelationReference{ |
| 443 | { |
| 444 | Type: sourceEntityType, |
| 445 | Relation: relationName, |
| 446 | }, |
| 447 | }, nil |
| 448 | } |
| 449 | |
| 450 | // Use BFS to find multi-hop path |
| 451 | visited := make(map[string]bool) |
| 452 | queue := []struct { |
| 453 | entityType string |
| 454 | path []*base.RelationReference |
| 455 | }{{sourceEntityType, []*base.RelationReference{}}} |
| 456 | |
| 457 | visited[sourceEntityType] = true |
| 458 | |
| 459 | for len(queue) > 0 { |
| 460 | current := queue[0] |
| 461 | queue = queue[1:] |
| 462 | |
| 463 | if current.entityType == targetEntityType { |
| 464 | return current.path, nil |
| 465 | } |
| 466 | |
| 467 | entityDef, exists := g.schema.EntityDefinitions[current.entityType] |
| 468 | if !exists { |
| 469 | continue |
| 470 | } |
| 471 | |
| 472 | // Explore all relations from current entity |
| 473 | for relationName, relationDef := range entityDef.Relations { |
| 474 | for _, relRef := range relationDef.RelationReferences { |
| 475 | nextEntityType := relRef.GetType() |
| 476 | if !visited[nextEntityType] { |
| 477 | visited[nextEntityType] = true |
| 478 | |
| 479 | // Build new path |
| 480 | newPath := make([]*base.RelationReference, len(current.path)+1) |
| 481 | copy(newPath, current.path) |
| 482 | newPath[len(current.path)] = &base.RelationReference{ |
| 483 | Type: current.entityType, |
| 484 | Relation: relationName, |
| 485 | } |
| 486 | |
| 487 | queue = append(queue, struct { |
| 488 | entityType string |
| 489 | path []*base.RelationReference |
| 490 | }{nextEntityType, newPath}) |
| 491 | } |
| 492 | } |
| 493 | } |
| 494 | } |
no test coverage detected