Skip to content

Commit

Permalink
Started to add logic to get an entity.
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbdz committed Sep 21, 2024
1 parent bad1b8f commit 3c166d3
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 3 deletions.
74 changes: 73 additions & 1 deletion entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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) {
Expand Down
16 changes: 15 additions & 1 deletion entity/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down
9 changes: 8 additions & 1 deletion entity/service.go
Original file line number Diff line number Diff line change
@@ -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{},
}
}
1 change: 1 addition & 0 deletions entity/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 3c166d3

Please sign in to comment.