-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(config): add update configuration details
- Loading branch information
Showing
4 changed files
with
120 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,4 +24,5 @@ Render: null | |
Repo: null | ||
Revision: 1 | ||
Store: null | ||
Update: null | ||
Webapp: null |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package config | ||
|
||
import "github.com/qri-io/jsonschema" | ||
|
||
// Update configures a Remote Procedure Call (Update) listener | ||
type Update struct { | ||
Daemonize bool `json:"daemonize"` | ||
Port int `json:"port"` | ||
} | ||
|
||
// DefaultUpdatePort is local the port Update serves on by default | ||
var DefaultUpdatePort = 2506 | ||
|
||
// DefaultUpdate creates a new default Update configuration | ||
func DefaultUpdate() *Update { | ||
return &Update{ | ||
Daemonize: true, | ||
Port: DefaultUpdatePort, | ||
} | ||
} | ||
|
||
// Validate validates all fields of rpc returning all errors found. | ||
func (cfg Update) Validate() error { | ||
schema := jsonschema.Must(` | ||
{ | ||
"$schema": "http://json-schema.org/draft-06/schema#", | ||
"title": "Update", | ||
"description": "The Update configuration", | ||
"type": "object", | ||
"required": ["daemonize", "port"], | ||
"properties": { | ||
"deamonize": { | ||
"description": "When true, the update service starts as a daemonized process", | ||
"type": "boolean" | ||
}, | ||
"port": { | ||
"description": "port update service will listen for rpc calls", | ||
"type": "integer" | ||
} | ||
} | ||
}`) | ||
return validate(schema, &cfg) | ||
} | ||
|
||
// Copy makes a deep copy of the Update struct | ||
func (cfg *Update) Copy() *Update { | ||
res := &Update{ | ||
Daemonize: cfg.Daemonize, | ||
Port: cfg.Port, | ||
} | ||
|
||
return res | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package config | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestUpdateValidate(t *testing.T) { | ||
err := DefaultUpdate().Validate() | ||
if err != nil { | ||
t.Errorf("error validating default update: %s", err) | ||
} | ||
} | ||
|
||
func TestUpdateCopy(t *testing.T) { | ||
cases := []struct { | ||
rpc *Update | ||
}{ | ||
{DefaultUpdate()}, | ||
} | ||
for i, c := range cases { | ||
cpy := c.rpc.Copy() | ||
if !reflect.DeepEqual(cpy, c.rpc) { | ||
t.Errorf("Update Copy test case %v, rpc structs are not equal: \ncopy: %v, \noriginal: %v", i, cpy, c.rpc) | ||
continue | ||
} | ||
cpy.Daemonize = false | ||
if reflect.DeepEqual(cpy, c.rpc) { | ||
t.Errorf("Update Copy test case %v, editing one rpc struct should not affect the other: \ncopy: %v, \noriginal: %v", i, cpy, c.rpc) | ||
continue | ||
} | ||
} | ||
} |