Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read parameters from JSON string #77

Merged
merged 3 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
## [[unpublished]](https://github.com/mlange-42/beecs/compare/v0.2.0...main)

### Breaking changes

* `FromJSON` for default and custom parameters renamed to `FromJSONFile` (#77)

### Features

- Adds option to terminate on extinction of all bees (#75)
- Adds observer `Extinction` to report the tick of colony extinction (#76)
- Adds `FromJSON([]byte)` for default and custom parameters (#77)

### Documentation

Expand Down
2 changes: 1 addition & 1 deletion _examples/json_parameters/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func main() {
// Get the default parameters.
p := params.Default()
// Read JSON to modify some parameters.
err := p.FromJSON("_examples/json_parameters/params.json")
err := p.FromJSONFile("_examples/json_parameters/params.json")
if err != nil {
log.Fatal(err)
}
Expand Down
20 changes: 14 additions & 6 deletions params/custom.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,24 +38,32 @@ type customParamsJs struct {
Custom map[string]entry
}

// FromJSON fills the parameter set with values from a JSON file.
// FromJSONFile fills the parameter set with values from a JSON file.
//
// Only values present in the file are overwritten,
// all other values remain unchanged.
func (p *CustomParams) FromJSON(path string) error {
file, err := os.Open(path)
func (p *CustomParams) FromJSONFile(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}
defer file.Close()

decoder := json.NewDecoder(file)
return p.FromJSON(content)
}

// FromJSON fills the parameter set with values from JSON.
//
// Only values present in the file are overwritten,
// all other values remain unchanged.
func (p *CustomParams) FromJSON(data []byte) error {
reader := bytes.NewReader(data)
decoder := json.NewDecoder(reader)
decoder.DisallowUnknownFields()

pars := customParamsJs{
Parameters: p.Parameters,
}
err = decoder.Decode(&pars)
err := decoder.Decode(&pars)
if err != nil {
return err
}
Expand Down
47 changes: 47 additions & 0 deletions params/custom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package params_test

import (
"reflect"
"testing"

"github.com/mlange-42/beecs/params"
"github.com/mlange-42/beecs/registry"
"github.com/stretchr/testify/assert"
)

type CustomParam struct {
Value int
}

func TestCustomParamsFromJSON(t *testing.T) {
registry.RegisterResource[CustomParam]()

p := params.CustomParams{}

js := `{
"Parameters": {
"Termination": {
"MaxTicks": 3650,
"OnExtinction": true
}
},
"Custom": {
"params_test.CustomParam": {
"Value": 100
}
}
}`

err := p.FromJSON([]byte(js))
assert.Nil(t, err)

assert.Equal(t, 3650, p.Parameters.Termination.MaxTicks)

obj, ok := p.Custom[reflect.TypeOf(CustomParam{})]
assert.True(t, ok)

par, ok := obj.(*CustomParam)
assert.True(t, ok)

assert.Equal(t, 100, par.Value)
}
23 changes: 17 additions & 6 deletions params/default.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package params

import (
"bytes"
"encoding/json"
"math/rand"
"os"
Expand All @@ -15,7 +16,9 @@ type Params interface {
// Apply the parameters to a world.
Apply(world *ecs.World)
// FromJSON fills the parameter set with values from a JSON file.
FromJSON(path string) error
FromJSONFile(path string) error
// FromJSON fills the parameter set with values from a JSON file.
FromJSON(data []byte) error
}

// DefaultParams contains all default parameters of BEEHAVE.
Expand Down Expand Up @@ -198,18 +201,26 @@ func Default() DefaultParams {
}
}

// FromJSON fills the parameter set with values from a JSON file.
// unchanged fills the parameter set with values from a JSON file.
//
// Only values present in the file are overwritten,
// all other values remain unchanged.
func (p *DefaultParams) FromJSON(path string) error {
file, err := os.Open(path)
func (p *DefaultParams) FromJSONFile(path string) error {
content, err := os.ReadFile(path)
if err != nil {
return err
}
defer file.Close()

decoder := json.NewDecoder(file)
return p.FromJSON(content)
}

// FromJSON fills the parameter set with values from JSON.
//
// Only values present in the file are overwritten,
// all other values remain unchanged.
func (p *DefaultParams) FromJSON(data []byte) error {
reader := bytes.NewReader(data)
decoder := json.NewDecoder(reader)
decoder.DisallowUnknownFields()
return decoder.Decode(p)
}
Expand Down
24 changes: 24 additions & 0 deletions params/default_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package params_test

import (
"testing"

"github.com/mlange-42/beecs/params"
"github.com/stretchr/testify/assert"
)

func TestDefaultParamsFromJSON(t *testing.T) {
p := params.Default()

js := `{
"Termination": {
"MaxTicks": 3650,
"OnExtinction": true
}
}`

err := p.FromJSON([]byte(js))
assert.Nil(t, err)

assert.Equal(t, 3650, p.Termination.MaxTicks)
}
Loading