Skip to content

Commit

Permalink
Added extract _self entity section and added the section in get
Browse files Browse the repository at this point in the history
  • Loading branch information
jnbdz committed Sep 21, 2024
1 parent 893ceb5 commit bad1b8f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 6 deletions.
25 changes: 23 additions & 2 deletions entity/get/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/AmadlaOrg/hery/entity"
"github.com/AmadlaOrg/hery/entity/build"
schemaPkg "github.com/AmadlaOrg/hery/entity/schema"
"github.com/AmadlaOrg/hery/entity/validation"
"github.com/AmadlaOrg/hery/entity/version"
versionValidationPkg "github.com/AmadlaOrg/hery/entity/version/validation"
Expand All @@ -28,6 +29,7 @@ type SGet struct {
EntityVersion version.IVersion
EntityVersionValidation versionValidationPkg.IValidation
Build build.IBuild
Schema schemaPkg.ISchema
}

// GetInTmp retrieves entities based on the provided collection name and entities
Expand Down Expand Up @@ -104,16 +106,35 @@ func (s *SGet) download(collectionName string, storagePaths *storage.AbsPaths, e
return
}

if selfEntity := s.Schema.ExtractSelfEntity(heryContent); selfEntity != nil {
selfEntitySchemaPath := s.Schema.GenerateSchemaPath(collectionName, entityMeta.AbsPath)
selfEntitySchema, err := s.Schema.Load(selfEntitySchemaPath)
if err != nil {
errCh <- fmt.Errorf("error loading schema: %v", err)
return
}

err = s.EntityValidation.Entity(collectionName, selfEntitySchema, selfEntity)
if err != nil {
errCh <- fmt.Errorf("error validating entity: %v", err)
return
}

// TODO: Remove `heryContent["_self"].(any)` so to not need to make it overly heavy for the validator
}

// TODO: Add validation to the main entity

// 3. Validate the content of hery file content to make sure it does not cause is issue later in the code
//
// -- This follows the Fail Fast principal --
//
// TODO:
err = s.EntityValidation.Entity(entityMeta.AbsPath, collectionName, entityMeta.Entity, heryContent)
/*err = s.EntityValidation.Entity(entityMeta.AbsPath, collectionName, entityMeta.Entity, heryContent)
if err != nil {
errCh <- fmt.Errorf("error validating entity: %v", err)
return
}
}*/

// 4. The reference to the other entities are found in the hery file content
//
Expand Down
10 changes: 10 additions & 0 deletions entity/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
// ISchema used by mockery
type ISchema interface {
Load(schemaPath string) (*jsonschema.Schema, error)
ExtractSelfEntity(heryContent map[string]any) map[string]any
GenerateSchemaPath(collectionName, entityPath string) string
GenerateURNPrefix(collectionName string) string
GenerateURN(urnPrefix, entityUri string) string
Expand Down Expand Up @@ -70,6 +71,15 @@ func (s *SSchema) Load(schemaPath string) (*jsonschema.Schema, error) {
return schema, nil
}

// ExtractSelfEntity returns the `_self` reserved property content
// If no content found then it will return `nil`
func (s *SSchema) ExtractSelfEntity(heryContent map[string]any) map[string]any {
if self, ok := heryContent["_self"].(any); ok {
return self.(map[string]any)
}
return nil
}

// GenerateSchemaPath returns the absolute path for the entity's schema
func (s *SSchema) GenerateSchemaPath(collectionName, entityPath string) string {
return filepath.Join(entityPath, "."+collectionName, EntityJsonSchemaFileName)
Expand Down
5 changes: 1 addition & 4 deletions entity/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,10 @@ func (s *SValidation) RootEntity(rootSchema, selfSchema *jsonschema.Schema, hery
}

// Entity validates the YAML content against the JSON schema
// TODO: Make sure that YAML standard is valid first
// TODO: Since JSON-Schema cannot merge by-it-self the schemas you will need to add code for that
// TODO: Make sure it validates properly with both the based schema found in `.schema` and the entity's own `schema.json`
// TODO: --- Used it
func (s *SValidation) Entity(collectionName string, schema *jsonschema.Schema, heryContent map[string]any) error {

// TODO: We need to add a unit test to see what happens when a YAML is not valid in the `.hery` content
// |-- TODO: Make sure that YAML standard is valid first

// 1. Get the schema of the entity and load the jsonschema
// TODO: Move outside so that it is added to type Entity
Expand Down

0 comments on commit bad1b8f

Please sign in to comment.