Skip to content

Commit

Permalink
Refactor to use snake_case in API models
Browse files Browse the repository at this point in the history
Also, move server types into file separate from server.

Fixes #222
  • Loading branch information
tsandall committed Jan 20, 2017
1 parent 6462f36 commit 7fa8a1e
Show file tree
Hide file tree
Showing 10 changed files with 750 additions and 770 deletions.
6 changes: 3 additions & 3 deletions ast/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ const (

// Error represents a single error caught during parsing, compiling, etc.
type Error struct {
Code ErrCode
Location *Location
Message string
Code ErrCode `json:"code"`
Location *Location `json:"location"`
Message string `json:"message"`
}

func (e *Error) Error() string {
Expand Down
34 changes: 17 additions & 17 deletions ast/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ type (
// within a namespace (defined by the package) and optional
// dependencies on external documents (defined by imports).
Module struct {
Package *Package
Imports []*Import
Rules []*Rule
Package *Package `json:"package"`
Imports []*Import `json:"imports,omitempty"`
Rules []*Rule `json:"rules,omitempty"`
}

// Comment contains the raw text from the comment in the definition.
Expand All @@ -90,44 +90,44 @@ type (
// by rules inside the module.
Package struct {
Location *Location `json:"-"`
Path Ref
Path Ref `json:"path"`
}

// Import represents a dependency on a document outside of the policy
// namespace. Imports are optional.
Import struct {
Location *Location `json:"-"`
Path *Term
Alias Var `json:",omitempty"`
Path *Term `json:"path"`
Alias Var `json:"alias,omitempty"`
}

// Rule represents a rule as defined in the language. Rules define the
// content of documents that represent policy decisions.
Rule struct {
Location *Location `json:"-"`
Name Var
Key *Term `json:",omitempty"`
Value *Term `json:",omitempty"`
Body Body
Name Var `json:"name"`
Key *Term `json:"key,omitempty"`
Value *Term `json:"value,omitempty"`
Body Body `json:"body"`
}

// Head represents the head of a rule.
// TODO(tsandall): refactor Rule to contain a Head.
Head struct {
Name Var
Key *Term
Value *Term
Name Var `json:"name"`
Key *Term `json:"key,omitempty"`
Value *Term `json:"value,omitempty"`
}

// Body represents one or more expressios contained inside a rule.
Body []*Expr

// Expr represents a single expression contained inside the body of a rule.
Expr struct {
Location *Location `json:"-"`
Index int
Negated bool `json:",omitempty"`
Terms interface{}
Location *Location `json:"-"`
Index int `json:"index"`
Negated bool `json:"negated,omitempty"`
Terms interface{} `json:"terms"`
}
)

Expand Down
26 changes: 13 additions & 13 deletions ast/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,43 +248,43 @@ func TestExprBadJSON(t *testing.T) {

js := `
{
"Negated": 100,
"Terms": {
"Value": "foo",
"Type": "string"
"negated": 100,
"terms": {
"value": "foo",
"type": "string"
},
"Index": 0
"index": 0
}
`

exp := fmt.Errorf("ast: unable to unmarshal Negated field with type: json.Number (expected true or false)")
exp := fmt.Errorf("ast: unable to unmarshal negated field with type: json.Number (expected true or false)")
assert(js, exp)

js = `
{
"Terms": [
"terms": [
"foo"
],
"Index": 0
"index": 0
}
`
exp = fmt.Errorf("ast: unable to unmarshal term")
assert(js, exp)

js = `
{
"Terms": "bad value",
"Index": 0
"terms": "bad value",
"index": 0
}
`
exp = fmt.Errorf(`ast: unable to unmarshal Terms field with type: string (expected {"Value": ..., "Type": ...} or [{"Value": ..., "Type": ...}, ...])`)
exp = fmt.Errorf(`ast: unable to unmarshal terms field with type: string (expected {"value": ..., "type": ...} or [{"value": ..., "type": ...}, ...])`)
assert(js, exp)

js = `
{
"Terms": {"Value": "foo", "Type": "string"}
"terms": {"value": "foo", "type": "string"}
}`
exp = fmt.Errorf("ast: unable to unmarshal Index field with type: <nil> (expected integer)")
exp = fmt.Errorf("ast: unable to unmarshal index field with type: <nil> (expected integer)")
assert(js, exp)
}

Expand Down
2 changes: 1 addition & 1 deletion ast/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// TypeName returns a human readable name for the AST element type.
func TypeName(x interface{}) string {
return strings.ToLower(reflect.TypeOf(x).Name())
return strings.ToLower(reflect.Indirect(reflect.ValueOf(x)).Type().Name())
}

// The type names provide consistent strings for types in error messages.
Expand Down
71 changes: 25 additions & 46 deletions ast/term.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ import (

// Location records a position in source code
type Location struct {
Text []byte `json:"-"` // The original text fragment from the source.
File string // The name of the source file (which may be empty).
Row int // The line in the source.
Col int // The column in the row.
Text []byte `json:"-"` // The original text fragment from the source.
File string `json:"file"` // The name of the source file (which may be empty).
Row int `json:"row"` // The line in the source.
Col int `json:"col"` // The column in the row.
}

// NewLocation returns a new Location object.
Expand Down Expand Up @@ -126,8 +126,8 @@ func InterfaceToValue(x interface{}) (Value, error) {

// Term is an argument to a function.
type Term struct {
Value Value // the value of the Term as represented in Go
Location *Location `json:"-"` // the location of the Term in the source
Value Value `json:"value"` // the value of the Term as represented in Go
Location *Location `json:"-"` // the location of the Term in the source
}

// NewTerm returns a new Term object.
Expand Down Expand Up @@ -193,32 +193,9 @@ func (term *Term) IsGround() bool {
//
// Specialized marshalling logic is required to include a type hint for Value.
func (term *Term) MarshalJSON() ([]byte, error) {
var typ string
switch term.Value.(type) {
case Null:
typ = "null"
case Boolean:
typ = "boolean"
case Number:
typ = "number"
case String:
typ = "string"
case Ref:
typ = "ref"
case Var:
typ = "var"
case Array:
typ = "array"
case Object:
typ = "object"
case *Set:
typ = "set"
case *ArrayComprehension:
typ = "array-comprehension"
}
d := map[string]interface{}{
"Type": typ,
"Value": term.Value,
"type": TypeName(term.Value),
"value": term.Value,
}
return json.Marshal(d)
}
Expand Down Expand Up @@ -907,8 +884,8 @@ func (obj Object) String() string {

// ArrayComprehension represents an array comprehension as defined in the language.
type ArrayComprehension struct {
Term *Term
Body Body
Term *Term `json:"term"`
Body Body `json:"body"`
}

// ArrayComprehensionTerm creates a new Term with an ArrayComprehension value.
Expand Down Expand Up @@ -1011,17 +988,17 @@ unmarshal_error:
}

func unmarshalExpr(expr *Expr, v map[string]interface{}) error {
if x, ok := v["Negated"]; ok {
if x, ok := v["negated"]; ok {
if b, ok := x.(bool); ok {
expr.Negated = b
} else {
return fmt.Errorf("ast: unable to unmarshal Negated field with type: %T (expected true or false)", v["Negated"])
return fmt.Errorf("ast: unable to unmarshal negated field with type: %T (expected true or false)", v["negated"])
}
}
if err := unmarshalExprIndex(expr, v); err != nil {
return err
}
switch ts := v["Terms"].(type) {
switch ts := v["terms"].(type) {
case map[string]interface{}:
t, err := unmarshalTerm(ts)
if err != nil {
Expand All @@ -1035,13 +1012,13 @@ func unmarshalExpr(expr *Expr, v map[string]interface{}) error {
}
expr.Terms = terms
default:
return fmt.Errorf(`ast: unable to unmarshal Terms field with type: %T (expected {"Value": ..., "Type": ...} or [{"Value": ..., "Type": ...}, ...])`, v["Terms"])
return fmt.Errorf(`ast: unable to unmarshal terms field with type: %T (expected {"value": ..., "type": ...} or [{"value": ..., "type": ...}, ...])`, v["terms"])
}
return nil
}

func unmarshalExprIndex(expr *Expr, v map[string]interface{}) error {
if x, ok := v["Index"]; ok {
if x, ok := v["index"]; ok {
if n, ok := x.(json.Number); ok {
i, err := n.Int64()
if err == nil {
Expand All @@ -1050,7 +1027,7 @@ func unmarshalExprIndex(expr *Expr, v map[string]interface{}) error {
}
}
}
return fmt.Errorf("ast: unable to unmarshal Index field with type: %T (expected integer)", v["Index"])
return fmt.Errorf("ast: unable to unmarshal index field with type: %T (expected integer)", v["index"])
}

func unmarshalTerm(m map[string]interface{}) (*Term, error) {
Expand All @@ -1068,6 +1045,8 @@ func unmarshalTermSlice(s []interface{}) ([]*Term, error) {
if t, err := unmarshalTerm(m); err == nil {
buf = append(buf, t)
continue
} else {
return nil, err
}
}
return nil, fmt.Errorf("ast: unable to unmarshal term")
Expand All @@ -1076,15 +1055,15 @@ func unmarshalTermSlice(s []interface{}) ([]*Term, error) {
}

func unmarshalTermSliceValue(d map[string]interface{}) ([]*Term, error) {
if s, ok := d["Value"].([]interface{}); ok {
if s, ok := d["value"].([]interface{}); ok {
return unmarshalTermSlice(s)
}
return nil, fmt.Errorf(`ast: unable to unmarshal term (expected {"Value": [...], "Type": ...} where type is one of: array, reference)`)
return nil, fmt.Errorf(`ast: unable to unmarshal term (expected {"value": [...], "type": ...} where type is one of: %v, %v, %v)`, ArrayTypeName, SetTypeName, RefTypeName)
}

func unmarshalValue(d map[string]interface{}) (Value, error) {
v := d["Value"]
switch d["Type"] {
v := d["value"]
switch d["type"] {
case "null":
return Null{}, nil
case "boolean":
Expand Down Expand Up @@ -1134,11 +1113,11 @@ func unmarshalValue(d map[string]interface{}) (Value, error) {
}
return buf, nil
}
case "array-comprehension":
case "arraycomprehension":
if m, ok := v.(map[string]interface{}); ok {
if t, ok := m["Term"].(map[string]interface{}); ok {
if t, ok := m["term"].(map[string]interface{}); ok {
if term, err := unmarshalTerm(t); err == nil {
if b, ok := m["Body"].([]interface{}); ok {
if b, ok := m["body"].([]interface{}); ok {
if body, err := unmarshalBody(b); err == nil {
buf := &ArrayComprehension{
Term: term,
Expand Down
Loading

0 comments on commit 7fa8a1e

Please sign in to comment.