func (test *TestAreaRepository) TestConvertFromLtreeFormat() { t := test.T() t.Parallel() resource.Require(t, resource.UnitTest) expected := "aaaaa1111-43284723hjkjhk-hkh432h423/aaaaaa22323232-4343434343/434343-4343sfsdfds-2423423" testString := "aaaaa1111_43284723hjkjhk_hkh432h423.aaaaaa22323232_4343434343.434343_4343sfsdfds_2423423" actual := area.ConvertFromLtreeFormat(testString) assert.Equal(t, expected, actual) }
func getResolvePath(appl application.Application, a *area.Area) (*string, error) { parentUuidStrings := strings.Split(area.ConvertFromLtreeFormat(a.Path), pathSepInService) parentUuids := convertToUuid(parentUuidStrings) parentAreas, err := appl.Areas().LoadMultiple(context.Background(), parentUuids) if err != nil { return nil, err } pathResolved := "" for _, a := range parentUuids { area := getAreaByID(a, parentAreas) if area == nil { continue } pathResolved = pathResolved + pathSepInService + area.Name } // Add the leading "/" in the "area1/area2/area3" styled path if pathResolved == "" { pathResolved = "/" } return &pathResolved, nil }
// ConvertArea converts between internal and external REST representation func ConvertArea(appl application.Application, request *goa.RequestData, ar *area.Area, additional ...AreaConvertFunc) *app.Area { areaType := area.APIStringTypeAreas spaceType := "spaces" spaceID := ar.SpaceID.String() selfURL := rest.AbsoluteURL(request, app.AreaHref(ar.ID)) childURL := rest.AbsoluteURL(request, app.AreaHref(ar.ID)+"/children") spaceSelfURL := rest.AbsoluteURL(request, app.SpaceHref(spaceID)) pathToTopMostParent := pathSepInService + area.ConvertFromLtreeFormat(ar.Path) // /uuid1/uuid2/uuid3s i := &app.Area{ Type: areaType, ID: &ar.ID, Attributes: &app.AreaAttributes{ Name: &ar.Name, CreatedAt: &ar.CreatedAt, Version: &ar.Version, ParentPath: &pathToTopMostParent, }, Relationships: &app.AreaRelations{ Space: &app.RelationGeneric{ Data: &app.GenericData{ Type: &spaceType, ID: &spaceID, }, Links: &app.GenericLinks{ Self: &spaceSelfURL, }, }, Children: &app.RelationGeneric{ Links: &app.GenericLinks{ Self: &childURL, }, }, }, Links: &app.GenericLinks{ Self: &selfURL, }, } // Now check the path, if the path is empty, then this is the topmost area // in a specific space. if ar.Path != "" { allParents := strings.Split(area.ConvertFromLtreeFormat(ar.Path), pathSepInService) parentID := allParents[len(allParents)-1] // Only the immediate parent's URL. parentSelfURL := rest.AbsoluteURL(request, app.AreaHref(parentID)) i.Relationships.Parent = &app.RelationGeneric{ Data: &app.GenericData{ Type: &areaType, ID: &parentID, }, Links: &app.GenericLinks{ Self: &parentSelfURL, }, } } for _, add := range additional { add(appl, request, ar, i) } return i }