Skip to content

Commit

Permalink
Changed the structure for entity and schema. Changed the way schemas …
Browse files Browse the repository at this point in the history
…are handled. Added some unit tests. Continue with the parsing.
  • Loading branch information
jnbdz committed Nov 19, 2024
1 parent bd98c4a commit 9453dd3
Show file tree
Hide file tree
Showing 13 changed files with 369 additions and 236 deletions.
82 changes: 78 additions & 4 deletions cache/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package parser

import (
"encoding/json"
"github.com/AmadlaOrg/hery/cache/database"
"github.com/AmadlaOrg/hery/entity"
"regexp"
Expand All @@ -16,6 +17,10 @@ type IParser interface {

type SParser struct{}

var (
jsonMarshal = json.Marshal
)

// Parse
func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
// TODO: Use schema to determine the data type for the SQL
Expand All @@ -24,6 +29,68 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
// TODO: For `Id` always: `Id TEXT PRIMARY KEY,`
// TODO: Maybe always have `NOT NULL` as a constrain. E.g.: name TEXT NOT NULL

// TODO: Handle different structures of _meta data
// TODO: Single entity:
/*
_meta:
_entity: github.com/AmadlaOrg/Entity@latest
_body:
name: RandomName
description: Some description.
category: QA
*/
// TODO: Or list:
/*
external-list:
- _entity: github.com/AmadlaOrg/QAFixturesSubEntityWithMultiSubEntities@latest
_body:
message: Another random message.
- _entity: github.com/AmadlaOrg/QAFixturesSubEntityWithMultiSubEntities@latest
_body:
message: Again, another random message.
- _entity: github.com/AmadlaOrg/QAFixturesEntityMultipleTagVersion@latest
_body:
title: Hello World!
- _entity: github.com/AmadlaOrg/QAFixturesEntityPseudoVersion@latest
_body:
name: John Doe
*/

// TODO: For UUID support
/*
CREATE TABLE example (
id TEXT PRIMARY KEY NOT NULL DEFAULT (lower(hex(randomblob(4)) || '-' || hex(randomblob(2)) || '-4' || substr(hex(randomblob(2)), 2) || '-' || substr('89ab', abs(random() % 4) + 1, 1) || substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6))))
);
*/

/*
JSON-Schema Types:
string.
number.
integer.
object.
array.
boolean.
null.
*/

/*
SQLite 3:
Declared Type Type Affinity Storage Class
INTEGER INTEGER INTEGER
TINYINT, SMALLINT INTEGER INTEGER
MEDIUMINT, BIGINT INTEGER INTEGER
INT INTEGER INTEGER
REAL, DOUBLE, FLOAT REAL REAL
NUMERIC, DECIMAL NUMERIC REAL or INTEGER (if possible)
TEXT TEXT TEXT
CHARACTER, VARCHAR TEXT TEXT
CLOB TEXT TEXT
BLOB BLOB BLOB
BOOLEAN NUMERIC INTEGER (1 for true, 0 for false)
DATE, DATETIME NUMERIC TEXT, REAL, or INTEGER depending on the format
*/

entityBody := entity.Content.Body

// TODO: It needs data type and constrain
Expand All @@ -44,6 +111,13 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
dynamicRelationships = append(dynamicRelationships, database.Relationships{})
}

// Convert schema map[string]any into a JSON string for cache storage
schemaJsonBytes, err := jsonMarshal(entity.Schema.Schema)
if err != nil {
return nil, err
}
schemaJsonString := string(schemaJsonBytes)

return []database.Table{
{
Name: "Entities",
Expand Down Expand Up @@ -107,8 +181,8 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
},
Rows: []map[string]any{
{
"Id": entity.Id,
"Entity": entity.Entity,
"Id": entity.Id.String(),
"Uri": entity.Uri,
"Name": entity.Name,
"RepoUrl": entity.RepoUrl,
"Origin": entity.Origin,
Expand All @@ -119,13 +193,13 @@ func (s *SParser) Entity(entity entity.Entity) ([]database.Table, error) {
"Have": entity.Have,
"Hash": entity.Hash,
"Exist": entity.Exist,
"Schema": ``,
"Schema": schemaJsonString,
"Content": ``,
},
},
},
{
Name: s.EntityToTableName(entity.Entity),
Name: s.EntityToTableName(entity.Uri),
Columns: []database.Column{
{},
},
Expand Down
9 changes: 5 additions & 4 deletions cache/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package parser
import (
"github.com/AmadlaOrg/hery/cache/database"
"github.com/AmadlaOrg/hery/entity"
"github.com/santhosh-tekuri/jsonschema/v6"
"github.com/AmadlaOrg/hery/entity/schema"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"testing"
)
Expand Down Expand Up @@ -140,8 +141,8 @@ func TestParseEntity(t *testing.T) {
},
}
e := entity.Entity{
Id: "c0fdd76d-a5b5-4f35-8784-e6238d6933ab",
Entity: "github.com/AmadlaOrg/EntityApplication/WebServer@v1.0.0",
Id: uuid.MustParse("c0fdd76d-a5b5-4f35-8784-e6238d6933ab"),
Uri: "github.com/AmadlaOrg/EntityApplication/WebServer@v1.0.0",
Name: "WebServer",
RepoUrl: "https://github.com/AmadlaOrg/EntityApplication",
Origin: "github.com/AmadlaOrg/EntityApplication",
Expand All @@ -152,7 +153,7 @@ func TestParseEntity(t *testing.T) {
Have: true,
Hash: "",
Exist: true,
Schema: &jsonschema.Schema{},
Schema: &schema.Schema{},
Content: entity.Content{
Entity: "github.com/AmadlaOrg/EntityApplication/WebServer@v1.0.0",
Id: "c0fdd76d-a5b5-4f35-8784-e6238d6933ab",
Expand Down
16 changes: 8 additions & 8 deletions entity/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ func (s *SBuild) Meta(paths storage.AbsPaths, entityUri string) (entity.Entity,
}
}

entityVals.AbsPath = filepath.Join(paths.Entities, entityVals.Entity)
entityVals.Id = uuidNew().String()
entityVals.AbsPath = filepath.Join(paths.Entities, entityVals.Uri)
entityVals.Id = uuidNew()
entityVals.Exist = true

return entityVals, nil
Expand Down Expand Up @@ -125,8 +125,8 @@ func (s *SBuild) metaFromLocalWithVersion(entityUri, entityVersion string) (enti
entityVals.IsPseudoVersion = false
entityVals.Name = filepath.Base(entityUriWithoutVersion)
entityVals.Version = entityVersion
entityVals.Entity = entityUri
entityVals.Origin = s.constructOrigin(entityVals.Entity, entityVals.Name, entityVals.Version)
entityVals.Uri = entityUri
entityVals.Origin = s.constructOrigin(entityVals.Uri, entityVals.Name, entityVals.Version)

return entityVals, nil
}
Expand Down Expand Up @@ -168,8 +168,8 @@ func (s *SBuild) metaFromRemoteWithoutVersion(entityUri string) (entity.Entity,

entityVals.Name = filepath.Base(entityUri)
entityVals.Version = entityVersion
entityVals.Entity = fmt.Sprintf("%s@%s", entityUri, entityVersion)
entityVals.Origin = s.constructOrigin(entityVals.Entity, entityVals.Name, entityVals.Version)
entityVals.Uri = fmt.Sprintf("%s@%s", entityUri, entityVersion)
entityVals.Origin = s.constructOrigin(entityVals.Uri, entityVals.Name, entityVals.Version)

return entityVals, nil
}
Expand Down Expand Up @@ -220,8 +220,8 @@ func (s *SBuild) metaFromRemoteWithVersion(entityUri, entityVersion string) (ent

entityVals.Name = filepath.Base(entityUriWithoutVersion)
entityVals.Version = entityVersion
entityVals.Entity = entityUri
entityVals.Origin = s.constructOrigin(entityVals.Entity, entityVals.Name, entityVals.Version)
entityVals.Uri = entityUri
entityVals.Origin = s.constructOrigin(entityVals.Uri, entityVals.Name, entityVals.Version)

return entityVals, nil
}
Expand Down
30 changes: 15 additions & 15 deletions entity/build/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/AmadlaOrg/hery/entity/validation"
"github.com/AmadlaOrg/hery/entity/version"
versionValidationPkg "github.com/AmadlaOrg/hery/entity/version/validation"
"github.com/AmadlaOrg/hery/message"
"github.com/AmadlaOrg/hery/storage"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -52,8 +53,8 @@ func TestMeta(t *testing.T) {
// No specific mocks needed for validation in this case
},
expectEntity: entity.Entity{
Id: "dca736d3-26c4-46b2-be5a-dfbdc09cff6d",
Entity: "github.com/example/entity@v1.0.0",
Id: uuid.MustParse("dca736d3-26c4-46b2-be5a-dfbdc09cff6d"),
Uri: "github.com/example/entity@v1.0.0",
Name: "entity",
RepoUrl: "https://github.com/example/entity",
Origin: "github.com/example/",
Expand All @@ -65,7 +66,6 @@ func TestMeta(t *testing.T) {
Hash: "",
Exist: true,
Schema: nil,
Config: nil,
},
hasError: false,
},
Expand All @@ -76,7 +76,7 @@ func TestMeta(t *testing.T) {
},
inputEntityUri: "github.com/example/entity@latest",
internalEntityDir: "",
internalEntityDirErr: entity.ErrorNotFound, // Not found since `latest` should never be used in directory name (only the latest static version or pseudo version)
internalEntityDirErr: message.ErrorNotFound, // Not found since `latest` should never be used in directory name (only the latest static version or pseudo version)
mockValidation: func(mockValidation *validation.MockEntityValidation) {
mockValidation.EXPECT().EntityUri("github.com/example/entity@latest").Return(true)
},
Expand All @@ -90,8 +90,8 @@ func TestMeta(t *testing.T) {
//mockEntityVersionVal.EXPECT().
},
expectEntity: entity.Entity{
Id: "dca736d3-26c4-46b2-be5a-dfbdc09cff6d",
Entity: "github.com/example/entity@v1.0.1",
Id: uuid.MustParse("dca736d3-26c4-46b2-be5a-dfbdc09cff6d"),
Uri: "github.com/example/entity@v1.0.1",
Name: "entity",
RepoUrl: "https://github.com/example/entity",
Origin: "github.com/example/",
Expand Down Expand Up @@ -214,7 +214,7 @@ func TestMeta(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockEntity := entity.MockEntity{}
/*mockEntity := entity.MockEntity{}
mockEntity.EXPECT().FindEntityDir(mock.Anything, mock.Anything).Return(
test.internalEntityDir, test.internalEntityDirErr)
Expand Down Expand Up @@ -243,7 +243,7 @@ func TestMeta(t *testing.T) {
if !reflect.DeepEqual(metaFromRemote, test.expectEntity) {
t.Errorf("expected: %v, got: %v", test.expectEntity, metaFromRemote)
}
}*/
})
}
}
Expand Down Expand Up @@ -271,7 +271,7 @@ func TestMetaFromRemoteWithoutVersion(t *testing.T) {
internalEntityVersionLatest: "v3.0.0",
internalEntityVersionLatestErr: nil,
expectEntity: entity.Entity{
Entity: "github.com/AmadlaOrg/Entity@v3.0.0",
Uri: "github.com/AmadlaOrg/Entity@v3.0.0",
Name: "Entity",
RepoUrl: "https://github.com/AmadlaOrg/Entity",
Origin: "github.com/AmadlaOrg/",
Expand All @@ -292,7 +292,7 @@ func TestMetaFromRemoteWithoutVersion(t *testing.T) {
internalEntityVersionLatest: "",
internalEntityVersionLatestErr: nil,
expectEntity: entity.Entity{
Entity: "github.com/AmadlaOrg/Entity@v0.0.0-20240823005443-9b4947da3948",
Uri: "github.com/AmadlaOrg/Entity@v0.0.0-20240823005443-9b4947da3948",
Name: "Entity",
RepoUrl: "https://github.com/AmadlaOrg/Entity",
Origin: "github.com/AmadlaOrg/",
Expand Down Expand Up @@ -425,7 +425,7 @@ func TestMetaFromRemoteWithVersion(t *testing.T) {
internalEntityVersionLatest: "v1.0.0",
internalEntityVersionLatestErr: nil,
expectEntity: entity.Entity{
Entity: "github.com/AmadlaOrg/Entity@v1.0.0",
Uri: "github.com/AmadlaOrg/Entity@v1.0.0",
Name: "Entity",
RepoUrl: "https://github.com/AmadlaOrg/Entity",
Origin: "github.com/AmadlaOrg/",
Expand All @@ -447,8 +447,8 @@ func TestMetaFromRemoteWithVersion(t *testing.T) {
internalEntityVersionLatest: "",
internalEntityVersionLatestErr: nil,
expectEntity: entity.Entity{
Id: "",
Entity: "github.com/AmadlaOrg/Entity@v0.0.0-20240823005443-9b4947da3948",
Id: uuid.MustParse("4c2b0c61-0850-4784-af36-11fda869f747"),
Uri: "github.com/AmadlaOrg/Entity@v0.0.0-20240823005443-9b4947da3948",
Name: "Entity",
RepoUrl: "https://github.com/AmadlaOrg/Entity",
Origin: "github.com/AmadlaOrg/",
Expand All @@ -475,7 +475,7 @@ func TestMetaFromRemoteWithVersion(t *testing.T) {
internalEntityVersionLatest: "v3.0.0",
internalEntityVersionLatestErr: nil,
expectEntity: entity.Entity{
Entity: "github.com/AmadlaOrg/Entity@v3.0.0",
Uri: "github.com/AmadlaOrg/Entity@v3.0.0",
Name: "Entity",
RepoUrl: "https://github.com/AmadlaOrg/Entity",
Origin: "github.com/AmadlaOrg/",
Expand Down Expand Up @@ -640,7 +640,7 @@ func TestMetaFromRemoteWithVersion(t *testing.T) {
internalEntityVersionLatest: "",
internalEntityVersionLatestErr: nil,
expectEntity: entity.Entity{
Entity: "",
Uri: "",
Name: "",
RepoUrl: "https://github.com/AmadlaOrg/Entity",
Origin: "",
Expand Down
6 changes: 3 additions & 3 deletions entity/entity.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ type SEntity struct {
func (s *SEntity) setContent(entity Entity, heryContent NotFormatedContent) (Content, error) {
// 1. Extract `_entity`
entitySection := heryContent["_entity"].(string)
if entity.Entity != "" {
entitySection = entity.Entity
if entity.Uri != "" {
entitySection = entity.Uri
} else if entitySection == "" {
return Content{}, errors.New("no entity section found")
}
Expand Down Expand Up @@ -118,7 +118,7 @@ func (s *SEntity) setContent(entity Entity, heryContent NotFormatedContent) (Con
// FindDir can find pseudo versioned entity directories and static versioned entities
func (s *SEntity) FindDir(paths storage.AbsPaths, entityVals Entity) (string, error) {
if !s.EntityVersionValidation.PseudoFormat(entityVals.Version) {
exactPath := entityVals.Entity
exactPath := entityVals.Uri

// Check if the directory exists
if _, err := osStat(exactPath); osIsNotExist(err) {
Expand Down
Loading

0 comments on commit 9453dd3

Please sign in to comment.