From a4a01b05c2d9943235dcc992742d3fa1e5a88cc0 Mon Sep 17 00:00:00 2001 From: Tyler J Cvetan Date: Mon, 14 Oct 2024 22:40:36 +0000 Subject: [PATCH] feat: better organization of game character - #5 --- .../core/domain/game/vtm_game_character.go | 27 ++++++++++++ .../vtm_game_character_test.go} | 26 +++++------ .../chronicle/core/domain/game_character.go | 44 +++++++------------ 3 files changed, 53 insertions(+), 44 deletions(-) create mode 100644 internal/chronicle/core/domain/game/vtm_game_character.go rename internal/chronicle/core/domain/{game_character_test.go => game/vtm_game_character_test.go} (61%) diff --git a/internal/chronicle/core/domain/game/vtm_game_character.go b/internal/chronicle/core/domain/game/vtm_game_character.go new file mode 100644 index 0000000..71dcf6b --- /dev/null +++ b/internal/chronicle/core/domain/game/vtm_game_character.go @@ -0,0 +1,27 @@ +package game + +import ( + "os" +) + +// TODO: I think this needs another sub property to work with the validator +type VtmGameCharacter struct { + Name string `json:"name"` + Disciplines []Discipline `json:"disciplines,omitempty"` +} + +type Discipline struct { + Name string `json:"name"` + Level int `json:"level"` + Powers []Power `json:"powers"` +} + +type Power struct { + Name string `json:"name"` + Level int `json:"level"` + Description string `json:"description,omitempty"` +} + +func (g VtmGameCharacter) Schema() ([]byte, error) { + return os.ReadFile("./vtm_v5_character_schema.json") +} diff --git a/internal/chronicle/core/domain/game_character_test.go b/internal/chronicle/core/domain/game/vtm_game_character_test.go similarity index 61% rename from internal/chronicle/core/domain/game_character_test.go rename to internal/chronicle/core/domain/game/vtm_game_character_test.go index 699ecca..8c5796c 100644 --- a/internal/chronicle/core/domain/game_character_test.go +++ b/internal/chronicle/core/domain/game/vtm_game_character_test.go @@ -1,23 +1,17 @@ -package domain_test +package game_test import ( "testing" "github.com/SomethingSexy/chronicle/internal/chronicle/core/domain" - "github.com/goccy/go-json" + "github.com/SomethingSexy/chronicle/internal/chronicle/core/domain/game" ) func TestGameCharacter_Validate_Valid(t *testing.T) { - var data interface{} - - if err := json.Unmarshal([]byte(`{ - "name": "John Doe" - }`), &data); err != nil { - t.Fatalf("Failed to unmarshal test cases: %v", err) - } - - gameCharacter := domain.GameCharacter[any]{ - Data: data, + gameCharacter := domain.GameCharacter[game.VtmGameCharacter]{ + Data: game.VtmGameCharacter{ + Name: "John Doe", + }, } valid, err := gameCharacter.Validate() @@ -32,13 +26,13 @@ func TestGameCharacter_Validate_Valid(t *testing.T) { } func TestGameCharacter_Validate_Disciplines_Valid(t *testing.T) { - gameCharacter := domain.GameCharacter[domain.VtmGameCharacter]{ - Data: domain.VtmGameCharacter{ + gameCharacter := domain.GameCharacter[game.VtmGameCharacter]{ + Data: game.VtmGameCharacter{ Name: "John Doe", - Disciplines: []domain.Discipline{{ + Disciplines: []game.Discipline{{ Name: "Protean", Level: 1, - Powers: []domain.Power{{ + Powers: []game.Power{{ Name: "Eyes of the Beast", Level: 1, Description: "See perfectly in total darkness with glowing red eyes.", diff --git a/internal/chronicle/core/domain/game_character.go b/internal/chronicle/core/domain/game_character.go index a9fdd11..d2c8598 100644 --- a/internal/chronicle/core/domain/game_character.go +++ b/internal/chronicle/core/domain/game_character.go @@ -1,29 +1,32 @@ package domain import ( - "fmt" - "os" + "log" "github.com/goccy/go-json" "github.com/kaptinlin/jsonschema" ) +type GameCharacterValidator interface { + Schema() ([]byte, error) +} + // This should probably be generic, need to // test this against the schema compiler though. // Maybe after it is valid, we can marshall to a strict type -type GameCharacter[D interface{}] struct { +type GameCharacter[D GameCharacterValidator] struct { Data D + Type GameType } func (g GameCharacter[D]) Validate() (bool, error) { - // For now just read on load (for testing purposes), we can deal with caching this later - var vtmV5CharacterSchema, err = os.ReadFile("./game/vtm_v5_character_schema.json") + characterSchema, err := g.Data.Schema() if err != nil { return false, err } compiler := jsonschema.NewCompiler() - schema, err := compiler.Compile(vtmV5CharacterSchema) + schema, err := compiler.Compile(characterSchema) if err != nil { return false, err } @@ -33,6 +36,7 @@ func (g GameCharacter[D]) Validate() (bool, error) { // For now we are going to marshall and unmarshall back into // a generic interface of maps so we can validate the schema // Hide it all here + log.Println(g.Data) dataAsByte, err := json.Marshal(g.Data) if err != nil { return false, err @@ -46,30 +50,14 @@ func (g GameCharacter[D]) Validate() (bool, error) { result := schema.Validate(dataAsInterface) if !result.IsValid() { - details, err := json.MarshalIndent(result.ToList(), "", " ") - if err != nil { - return false, err - } - fmt.Println(string(details)) + log.Println(result) + // details, err := json.MarshalIndent(result.ToList(), "", " ") + // if err != nil { + // return false, err + // } + // fmt.Println(string(details)) return false, nil } return true, nil } - -type VtmGameCharacter struct { - Name string `json:"name"` - Disciplines []Discipline `json:"disciplines"` -} - -type Discipline struct { - Name string `json:"name"` - Level int `json:"level"` - Powers []Power `json:"powers"` -} - -type Power struct { - Name string `json:"name"` - Level int `json:"level"` - Description string `json:"description,omitempty"` -}