Initial commit:

Added framework to generate DOT and png files of character map chart
from data folder.
Added all figures in the 1-2 chapters.
Added font file SimSum for render chart
This commit is contained in:
2024-09-02 04:59:36 +00:00
commit 8caf0ac3aa
36 changed files with 1236 additions and 0 deletions
+17
View File
@@ -0,0 +1,17 @@
package entity
import (
r "xyj-figures/pkg/relationship"
)
type Character struct {
Name string `yaml:"name"`
OtherNames []string `yaml:"other_names,flow"`
Relationships []r.Relationship `yaml:"relationships,flow"`
Description string `yaml:"description"`
Links []string `yaml:"links,flow"`
}
func (c Character) GetRelationships() []r.Relationship {
return c.Relationships
}
+94
View File
@@ -0,0 +1,94 @@
package entity
import (
"fmt"
"os"
r "xyj-figures/pkg/relationship"
yaml "gopkg.in/yaml.v3"
)
type Entity struct {
Schema string `yaml:"schema,flow"`
Type string `yaml:"type,flow"`
Details interface{} `yaml:"details,flow"`
}
type EntityType string
const (
EntityTypeCharacter = "character"
EntityTypeGroup = "group"
)
type EntityDetails interface {
GetRelationships() []r.Relationship
}
func NewEntity() (*Entity, error) {
return &Entity{Schema: "v1"}, nil
}
func Load(path string) (*Entity, error) {
f, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("failed to open file: %v", err)
}
e, _ := NewEntity()
if err = yaml.Unmarshal(f, &e); err != nil {
return nil, fmt.Errorf("failed to unmarshal file: %v", err)
}
switch e.Type {
case EntityTypeCharacter:
tmp, err := yaml.Marshal(e.Details)
if err != nil {
return nil, fmt.Errorf("failed to marshal filed %s: %v", EntityTypeCharacter, err)
}
c := Character{}
if err = yaml.Unmarshal(tmp, &c); err != nil {
return nil, fmt.Errorf("failed to unmarshal filed %s: %v", EntityTypeCharacter, err)
}
e.Details = c
case EntityTypeGroup:
tmp, err := yaml.Marshal(e.Details)
if err != nil {
return nil, fmt.Errorf("failed to marshal filed %s: %v", EntityTypeGroup, err)
}
g := Group{}
if err = yaml.Unmarshal(tmp, &g); err != nil {
return nil, fmt.Errorf("failed to unmarshal filed %s: %v", EntityTypeGroup, err)
}
e.Details = g
}
return e, nil
}
func (e Entity) Merge(other Entity) error {
if (e.Schema != other.Schema) || (e.Type != other.Type) {
return fmt.Errorf("unmatch merge between %v and %v", e, other)
}
switch e.Type {
case EntityTypeCharacter:
if e.Details.(Character).Name != other.Details.(Character).Name {
return fmt.Errorf("unmatch merge between %v and %v: names unmatch", e, other)
}
e.Details = Character{
Name: e.Details.(Character).Name,
OtherNames: append(e.Details.(Character).OtherNames, other.Details.(Character).OtherNames...),
Relationships: append(e.Details.(Character).Relationships, other.Details.(Character).Relationships...),
Description: fmt.Sprintf("%s%s", e.Details.(Character).Description, other.Details.(Character).Description),
Links: append(e.Details.(Character).Links, other.Details.(Character).Links...),
}
case EntityTypeGroup:
e.Details = Group{
Name: e.Details.(Group).Name,
OtherNames: append(e.Details.(Group).OtherNames, other.Details.(Group).OtherNames...),
Members: append(e.Details.(Group).Members, other.Details.(Group).Members...),
Relationships: append(e.Details.(Group).Relationships, other.Details.(Group).Relationships...),
Description: fmt.Sprintf("%s%s", e.Details.(Group).Description, other.Details.(Group).Description),
Links: append(e.Details.(Group).Links, other.Details.(Group).Links...),
}
}
return nil
}
+123
View File
@@ -0,0 +1,123 @@
package entity_test
import (
"log"
"os"
"path/filepath"
"reflect"
"testing"
"xyj-figures/pkg/entity"
"xyj-figures/pkg/relationship"
"github.com/go-test/deep"
)
func TestLoad(t *testing.T) {
tmpDir := os.TempDir()
tcs := []struct {
name string
payload string
want entity.Entity
wanterr bool
err error
}{
{
name: "Valid Character",
payload: `schema: v1
type: character
details:
name: 孙悟空
other_names:
- 孙行者
- 者行孙
relationships:
- source: 孙悟空
destination: 唐三藏
description: 师徒
bidirection: true
description: 孙悟空是明朝中后期畅销小说《西游记》当中个第一主角,是从石头里向蹦出来个猢狲。
links:
- https://zh.wikipedia.org/wiki/%E5%AD%99%E6%82%9F%E7%A9%BA
`,
want: entity.Entity{
Schema: "v1",
Type: entity.EntityTypeCharacter,
Details: entity.Character{
Name: "孙悟空",
OtherNames: []string{"孙行者", "者行孙"},
Relationships: []relationship.Relationship{
{
Src: "孙悟空",
Dst: "唐三藏",
Description: "师徒",
Bidirection: true,
},
},
Description: "孙悟空是明朝中后期畅销小说《西游记》当中个第一主角,是从石头里向蹦出来个猢狲。",
Links: []string{
"https://zh.wikipedia.org/wiki/%E5%AD%99%E6%82%9F%E7%A9%BA",
},
},
},
wanterr: false,
},
{
name: "Valid Group",
payload: `schema: v1
type: group
details:
name: 唐僧师徒
other_names:
- 取经团队
members:
- 孙悟空
- 猪八戒
- 沙僧
- 唐僧
- 白龙马
links:
- https://zh.wikipedia.org/wiki/%E8%A5%BF%E6%B8%B8%E8%AE%B0%E8%A7%92%E8%89%B2%E5%88%97%E8%A1%A8#%E5%94%90%E5%83%A7%E5%B8%AB%E5%BE%92
`,
want: entity.Entity{
Schema: "v1",
Type: entity.EntityTypeGroup,
Details: entity.Group{
Name: "唐僧师徒",
OtherNames: []string{"取经团队"},
Members: []string{"孙悟空", "猪八戒", "沙僧", "唐僧", "白龙马"},
Links: []string{
"https://zh.wikipedia.org/wiki/%E8%A5%BF%E6%B8%B8%E8%AE%B0%E8%A7%92%E8%89%B2%E5%88%97%E8%A1%A8#%E5%94%90%E5%83%A7%E5%B8%AB%E5%BE%92",
},
},
},
wanterr: false,
},
}
for _, tc := range tcs {
t.Run(tc.name, func(t *testing.T) {
// Prepare data
tmpFile, err := os.Create(filepath.Join(tmpDir, tc.name))
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}
defer tmpFile.Close()
if _, err = tmpFile.Write([]byte(tc.payload)); err != nil {
t.Fatalf("Failed to write to temporary file: %v", err)
}
// Call the function and assert the results
e, err := entity.Load(tmpFile.Name())
if err != nil && !tc.wanterr {
t.Errorf("Unexpected error: %v", err)
}
if err == nil && tc.wanterr {
t.Errorf("expected error but not get")
}
// Sanity Check
if !reflect.DeepEqual(*e, tc.want) {
log.Fatalf("Load got %+v want %+v, diff %+v", *e, tc.want, deep.Equal(*e, tc.want))
}
})
}
}
+16
View File
@@ -0,0 +1,16 @@
package entity
import r "xyj-figures/pkg/relationship"
type Group struct {
Name string
OtherNames []string `yaml:"other_names,flow"`
Members []string `yaml:"members,flow"`
Relationships []r.Relationship `yaml:"relationships,flow"`
Description string `yaml:"description"`
Links []string `yaml:"links,flow"`
}
func (g Group) GetRelationships() []r.Relationship {
return g.Relationships
}