2021-12-29 20:57:14 +00:00
|
|
|
// Package gg implements graph serialization to/from the gg text format.
|
2017-10-21 18:39:23 +00:00
|
|
|
package gg
|
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2021-12-29 19:32:53 +00:00
|
|
|
|
|
|
|
"github.com/mediocregopher/ginger/graph"
|
2021-12-26 23:23:41 +00:00
|
|
|
)
|
|
|
|
|
2021-12-28 16:49:02 +00:00
|
|
|
// ZeroValue is a Value with no fields set.
|
|
|
|
var ZeroValue Value
|
|
|
|
|
2021-12-29 20:57:14 +00:00
|
|
|
// Value represents a value which can be serialized by the gg text format.
|
2018-01-21 15:39:25 +00:00
|
|
|
type Value struct {
|
2021-12-28 16:49:02 +00:00
|
|
|
|
|
|
|
// Only one of these fields may be set
|
2021-12-27 17:11:07 +00:00
|
|
|
Name *string
|
|
|
|
Number *int64
|
2021-12-30 16:56:20 +00:00
|
|
|
Graph *Graph
|
2021-12-27 17:11:07 +00:00
|
|
|
|
|
|
|
// TODO coming soon!
|
|
|
|
// String *string
|
2021-12-28 16:49:02 +00:00
|
|
|
|
|
|
|
// Optional fields indicating the token which was used to construct this
|
|
|
|
// Value, if any.
|
|
|
|
LexerToken *LexerToken
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsZero returns true if the Value is the zero value (none of the sub-value
|
|
|
|
// fields are set). LexerToken is ignored for this check.
|
|
|
|
func (v Value) IsZero() bool {
|
2021-12-29 19:32:53 +00:00
|
|
|
return v.Equal(ZeroValue)
|
2017-10-21 18:39:23 +00:00
|
|
|
}
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
// Equal returns true if the passed in Value is equivalent, ignoring the
|
|
|
|
// LexerToken on either Value.
|
|
|
|
//
|
|
|
|
// Will panic if the passed in v2 is not a Value from this package.
|
|
|
|
func (v Value) Equal(v2g graph.Value) bool {
|
|
|
|
|
|
|
|
v2 := v2g.(Value)
|
|
|
|
|
|
|
|
v.LexerToken, v2.LexerToken = nil, nil
|
|
|
|
|
2021-12-27 17:11:07 +00:00
|
|
|
switch {
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
case v == ZeroValue && v2 == ZeroValue:
|
2021-12-27 21:20:50 +00:00
|
|
|
return true
|
|
|
|
|
2021-12-27 17:11:07 +00:00
|
|
|
case v.Name != nil && v2.Name != nil && *v.Name == *v2.Name:
|
|
|
|
return true
|
|
|
|
|
|
|
|
case v.Number != nil && v2.Number != nil && *v.Number == *v2.Number:
|
|
|
|
return true
|
|
|
|
|
2021-12-29 19:32:53 +00:00
|
|
|
case v.Graph != nil && v2.Graph != nil && v.Graph.Equal(v2.Graph):
|
2021-12-27 17:11:07 +00:00
|
|
|
return true
|
|
|
|
|
|
|
|
default:
|
|
|
|
return false
|
2018-01-21 15:39:25 +00:00
|
|
|
}
|
2017-11-24 18:05:58 +00:00
|
|
|
}
|
|
|
|
|
2021-12-26 23:23:41 +00:00
|
|
|
func (v Value) String() string {
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
case v.Name != nil:
|
|
|
|
return *v.Name
|
|
|
|
|
|
|
|
case v.Number != nil:
|
|
|
|
return fmt.Sprint(*v.Number)
|
|
|
|
|
|
|
|
case v.Graph != nil:
|
|
|
|
return v.Graph.String()
|
|
|
|
|
|
|
|
default:
|
2021-12-29 19:32:53 +00:00
|
|
|
return "<zero>"
|
2017-11-05 16:57:57 +00:00
|
|
|
}
|
2017-12-03 19:38:53 +00:00
|
|
|
}
|