~nromdotcom/gemif

ref: 0d4b60a8720279f402503491905f77a2a27b9d1e gemif/pkg/gamemanager/engineconfig.go -rw-r--r-- 1.3 KiB
0d4b60a8Norm MacLennan Add a few more tests 1 year, 5 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package gamemanager

import (
	"errors"
	"fmt"
)

// EngineConfig contains settings information for driving the engine.
type EngineConfig struct {
	RenderDescriptions bool
	StateTokenFormat   SerializationFormat
}

// SerializationFormat determines which format StateTokens are serialized as.
type SerializationFormat int

const (
	// JSON describes the json serilization format.
	JSON SerializationFormat = iota + 1
	// Protobuf describes the protobuf serilization format.
	Protobuf
)

// GetSerDeFormat translates format type strings to SerializationFormat.
func GetSerDeFormat(format string) SerializationFormat {
	serializationFormats := map[string]SerializationFormat{
		"json":  JSON,
		"proto": Protobuf,
	}

	return serializationFormats[format]
}

// ErrInvalidSerializationFormat occurs when engine config specifies a unsupported format.
var ErrInvalidSerializationFormat = errors.New("invalid serialization format")

// NewEngineConfig news up an EngineConfig based on passed in values.
func NewEngineConfig(renderDesc bool, tokenFormat string) (EngineConfig, error) {
	serdeFormat := GetSerDeFormat(tokenFormat)
	if serdeFormat == 0 {
		return EngineConfig{}, fmt.Errorf("%w: %s", ErrInvalidSerializationFormat, tokenFormat)
	}

	return EngineConfig{
		RenderDescriptions: renderDesc,
		StateTokenFormat:   serdeFormat,
	}, nil
}