• 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

92.59
/serialize.go
1
package archeserde
2

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

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

12
// Serialize an Arche ECS world to JSON.
13
func Serialize(world *ecs.World) ([]byte, error) {
2✔
14
        builder := strings.Builder{}
2✔
15

2✔
16
        builder.WriteString("{\"Components\" : [\n")
2✔
17

2✔
18
        types := map[ecs.ID]reflect.Type{}
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
                }
2✔
23
        }
24
        maxComp := len(types) - 1
2✔
25
        counter := 0
2✔
26
        for _, tp := range types {
4✔
27
                builder.WriteString(fmt.Sprintf("  \"%s\"", tp.String()))
2✔
28
                if counter < maxComp {
3✔
29
                        builder.WriteString(",")
1✔
30
                }
1✔
31
                builder.WriteString("\n")
2✔
32
                counter++
2✔
33
        }
34

35
        builder.WriteString("],\n\"Entities\" : [\n")
2✔
36
        query := world.Query(ecs.All())
2✔
37
        lastEntity := query.Count() - 1
2✔
38
        counter = 0
2✔
39
        for query.Next() {
4✔
40
                builder.WriteString("  {\n")
2✔
41

2✔
42
                ids := query.Ids()
2✔
43
                last := len(ids) - 1
2✔
44
                for i, id := range ids {
5✔
45
                        tp, _ := ecs.ComponentType(world, id)
3✔
46

3✔
47
                        comp := query.Get(id)
3✔
48
                        value := reflect.NewAt(tp, comp).Interface()
3✔
49
                        jsonData, err := json.Marshal(value)
3✔
50
                        if err != nil {
3✔
51
                                return nil, err
×
52
                        }
×
53
                        builder.WriteString("    ")
3✔
54
                        builder.WriteString(fmt.Sprintf("\"%s\" : ", tp.String()))
3✔
55
                        builder.WriteString(string(jsonData))
3✔
56
                        if i < last {
4✔
57
                                builder.WriteString(",")
1✔
58
                        }
1✔
59
                        builder.WriteString("\n")
3✔
60
                }
61

62
                builder.WriteString("  }")
2✔
63
                if counter < lastEntity {
3✔
64
                        builder.WriteString(",")
1✔
65
                }
1✔
66
                builder.WriteString("\n")
2✔
67

2✔
68
                counter++
2✔
69
        }
70
        builder.WriteString("],\n\"Resources\" : {\n")
2✔
71

2✔
72
        resTypes := map[ecs.ResID]reflect.Type{}
2✔
73
        for i := 0; i < ecs.MaskTotalBits; i++ {
514✔
74
                if tp, ok := ecs.ResourceType(world, ecs.ResID(i)); ok {
513✔
75
                        resTypes[ecs.ResID(i)] = tp
1✔
76
                }
1✔
77
        }
78

79
        last := len(resTypes) - 1
2✔
80
        counter = 0
2✔
81
        for id, tp := range resTypes {
3✔
82
                res := world.Resources().Get(id)
1✔
83
                rValue := reflect.ValueOf(res)
1✔
84
                ptr := rValue.UnsafePointer()
1✔
85

1✔
86
                value := reflect.NewAt(tp, ptr).Interface()
1✔
87
                jsonData, err := json.Marshal(value)
1✔
88
                if err != nil {
1✔
89
                        return nil, err
×
90
                }
×
91

92
                builder.WriteString("    ")
1✔
93
                builder.WriteString(fmt.Sprintf("\"%s\" : ", tp.String()))
1✔
94
                builder.WriteString(string(jsonData))
1✔
95

1✔
96
                if counter < last {
1✔
97
                        builder.WriteString(",")
×
98
                }
×
99
                builder.WriteString("\n")
1✔
100
        }
101

102
        builder.WriteString("}}")
2✔
103

2✔
104
        return []byte(builder.String()), nil
2✔
105
}
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