• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlange-42 / arche-serde / 7479011048

10 Jan 2024 06:06PM CUT coverage: 83.766%. Remained the same
7479011048

push

github

web-flow
Add examples to readme and package (#2)

* add examples to readme and package
* add features disclaimer

129 of 154 relevant lines covered (83.77%)

28.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.76
/deserialize.go
1
package archeserde
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "reflect"
7

8
        "github.com/mlange-42/arche/ecs"
9
)
10

11
// Deserialize an Arche [ecs.World] from JSON.
12
//
13
// The world must be prepared the following way:
14
//   - All required component types must be registered using [ecs.ComponentID]
15
//   - All required resources must be added using [ecs.AddResource]
16
func Deserialize(jsonData []byte, world *ecs.World) error {
2✔
17
        types := map[ecs.ID]reflect.Type{}
2✔
18
        ids := map[string]ecs.ID{}
2✔
19
        for i := 0; i < ecs.MaskTotalBits; i++ {
514✔
20
                if tp, ok := ecs.ComponentType(world, ecs.ID(i)); ok {
514✔
21
                        types[ecs.ID(i)] = tp
2✔
22
                        ids[tp.String()] = ecs.ID(i)
2✔
23
                }
2✔
24
        }
25

26
        resTypes := map[ecs.ResID]reflect.Type{}
2✔
27
        resIds := map[string]ecs.ResID{}
2✔
28
        for i := 0; i < ecs.MaskTotalBits; i++ {
514✔
29
                if tp, ok := ecs.ResourceType(world, ecs.ResID(i)); ok {
513✔
30
                        resTypes[ecs.ResID(i)] = tp
1✔
31
                        resIds[tp.String()] = ecs.ResID(i)
1✔
32
                }
1✔
33
        }
34

35
        deserial := deserializer{}
2✔
36

2✔
37
        if err := json.Unmarshal(jsonData, &deserial); err != nil {
2✔
38
                return err
×
39
        }
×
40

41
        for _, tp := range deserial.Components {
4✔
42
                if _, ok := ids[tp]; !ok {
2✔
43
                        return fmt.Errorf("component type is not registered: %s", tp)
×
44
                }
×
45
        }
46

47
        for _, e := range deserial.Entities {
4✔
48
                mp := map[string]entry{}
2✔
49

2✔
50
                if err := json.Unmarshal(e.Bytes, &mp); err != nil {
2✔
51
                        return err
×
52
                }
×
53

54
                components := []ecs.Component{}
2✔
55
                for tpName, value := range mp {
5✔
56
                        id := ids[tpName]
3✔
57
                        tp := types[id]
3✔
58

3✔
59
                        component := reflect.New(tp).Interface()
3✔
60
                        if err := json.Unmarshal(value.Bytes, &component); err != nil {
3✔
61
                                return err
×
62
                        }
×
63
                        components = append(components, ecs.Component{
3✔
64
                                ID:   id,
3✔
65
                                Comp: component,
3✔
66
                        })
3✔
67
                }
68
                _ = world.NewEntityWith(components...)
2✔
69
        }
70

71
        for tpName, res := range deserial.Resources {
3✔
72
                resID, ok := resIds[tpName]
1✔
73
                if !ok {
1✔
74
                        return fmt.Errorf("resource type is not registered: %s", tpName)
×
75
                }
×
76

77
                tp := resTypes[resID]
1✔
78
                resource := reflect.New(tp).Interface()
1✔
79
                if err := json.Unmarshal(res.Bytes, &resource); err != nil {
1✔
80
                        return err
×
81
                }
×
82

83
                resLoc := world.Resources().Get(resID)
1✔
84
                if resLoc == nil {
1✔
85
                        return fmt.Errorf("resource type registered but nil: %s", tpName)
×
86
                }
×
87

88
                rValue := reflect.ValueOf(resLoc)
1✔
89
                ptr := rValue.UnsafePointer()
1✔
90
                value := reflect.NewAt(tp, ptr).Interface()
1✔
91

1✔
92
                if err := json.Unmarshal(res.Bytes, &value); err != nil {
1✔
93
                        return err
×
94
                }
×
95
        }
96

97
        return nil
2✔
98
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc