From 3c166d3e05d96808986911652d9748b2feffbb52 Mon Sep 17 00:00:00 2001 From: jnbdz Date: Sat, 21 Sep 2024 17:33:07 -0400 Subject: [PATCH] Started to add logic to get an entity. --- entity/entity.go | 74 ++++++++++++++++++++++++++++++++++++++++++++++- entity/get/get.go | 16 +++++++++- entity/service.go | 9 +++++- entity/types.go | 1 + 4 files changed, 97 insertions(+), 3 deletions(-) diff --git a/entity/entity.go b/entity/entity.go index 1af356a..e955f99 100644 --- a/entity/entity.go +++ b/entity/entity.go @@ -7,10 +7,12 @@ import ( versionValidationPkg "github.com/AmadlaOrg/hery/entity/version/validation" "github.com/AmadlaOrg/hery/storage" "github.com/AmadlaOrg/hery/util/file" + "github.com/AmadlaOrg/hery/util/url" "gopkg.in/yaml.v3" "os" "path/filepath" "regexp" + "strings" "sync" ) @@ -22,7 +24,77 @@ type IEntity interface { Read(path, collectionName string) (map[string]any, error) } -type SEntity struct{} +type SEntity struct { + EntityVersion version.IVersion + + // Data + Entities []Entity +} + +// GetEntity +func (s *SEntity) GetEntity(entityUri string) (Entity, error) { + var ( + entityVals = Entity{ + Have: false, + Exist: false, + } + //err error + ) + + if strings.Contains(entityUri, "@") { + entityVersion, err := s.EntityVersion.Extract(entityUri) + if err != nil { + if !errors.Is(err, version.ErrorExtractNoVersionFound) { + return entityVals, fmt.Errorf("error extracting version: %v", err) + } else { + entityVersion = "latest" + } + } + + entityUriWithoutVersion := url.TrimVersion(entityUri, entityVersion) + entityVals.RepoUrl, err = url.ExtractRepoUrl(entityUriWithoutVersion) + if err != nil { + return entityVals, fmt.Errorf("error extracting repo url: %v", err) + } + + if entityVersion == "latest" { + for _, entity := range s.Entities { + if entity.LatestVersion && entity.RepoUrl == entityVals.RepoUrl { + return entity, nil + } + } + } else { + for _, entity := range s.Entities { + if !entity.LatestVersion && entity.Version == entityVersion && entity.RepoUrl == entityVals.RepoUrl { + return entity, nil + } + } + } + } else { + var ( + entityVals = entity.Entity{ + Have: false, + Exist: false, + } + err error + ) + + entityUriWithoutVersion := url.TrimVersion(entityUri, entityVersion) + entityVals.RepoUrl, err = url.ExtractRepoUrl(entityUriWithoutVersion) + + if err != nil { + return entityVals, fmt.Errorf("error extracting repo url: %v", err) + } + + entityVersionList, err := s.EntityVersion.List(entityVals.RepoUrl) + if err != nil { + return entityVals, fmt.Errorf("error listing versions: %v", err) + } + } + + /**/ + return Entity{}, fmt.Errorf("no entity found with uri: %s", entityUri) +} // FindEntityDir can find pseudo versioned entity directories and static versioned entities. func (s *SEntity) FindEntityDir(paths storage.AbsPaths, entityVals Entity) (string, error) { diff --git a/entity/get/get.go b/entity/get/get.go index 69678d8..fde1f94 100644 --- a/entity/get/get.go +++ b/entity/get/get.go @@ -106,7 +106,12 @@ func (s *SGet) download(collectionName string, storagePaths *storage.AbsPaths, e return } + // 3. Entity validation + // 3.1: Retrieve the `_entity` + + // 3.2: Extracted _self entity validation if selfEntity := s.Schema.ExtractSelfEntity(heryContent); selfEntity != nil { + selfEntitySchemaPath := s.Schema.GenerateSchemaPath(collectionName, entityMeta.AbsPath) selfEntitySchema, err := s.Schema.Load(selfEntitySchemaPath) if err != nil { @@ -120,7 +125,16 @@ func (s *SGet) download(collectionName string, storagePaths *storage.AbsPaths, e return } - // TODO: Remove `heryContent["_self"].(any)` so to not need to make it overly heavy for the validator + delete(heryContent, "_self") + + err = s.EntityValidation.Entity(collectionName, selfEntitySchema, selfEntity) + if err != nil { + errCh <- fmt.Errorf("error validating entity: %v", err) + return + } + } else { + // No `_self` + } // TODO: Add validation to the main entity diff --git a/entity/service.go b/entity/service.go index 8efde05..6b6c2d9 100644 --- a/entity/service.go +++ b/entity/service.go @@ -1,6 +1,13 @@ package entity +import "github.com/AmadlaOrg/hery/entity/version" + // NewEntityService to set up the entity build service func NewEntityService() *SEntity { - return &SEntity{} + return &SEntity{ + EntityVersion: version.NewEntityVersionService(), + + // Data + Entities: []Entity{}, + } } diff --git a/entity/types.go b/entity/types.go index a59d364..291462a 100644 --- a/entity/types.go +++ b/entity/types.go @@ -26,6 +26,7 @@ type Entity struct { RepoUrl string // The full repository URL (e.g.: https://github.com/AmadlaOrg/EntityApplication) Origin string // The entity URL path (it can also be used as a relative path) (e.g.: github.com/AmadlaOrg/EntityApplication) Version string // The entity version (what is after `@`) (e.g.: v1.0.0) + LatestVersion bool // Indicates if the Version of this entity is the most recent IsPseudoVersion bool // True if the version was generated AbsPath string // The absolute path to where the entity is stored (e.g.: /home/user/.hery/amadla/entity/github.com/AmadlaOrg/EntityApplication/WebServer@v1.0.0) Have bool // True if the entity is downloaded and false if not (e.g.: true)