2019-06-17 09:48:05 +00:00
|
|
|
package parser
|
2018-12-04 13:24:04 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-11-28 20:56:04 +00:00
|
|
|
// EncoderToNodeOpts Options for the encoderToNode.
|
|
|
|
type EncoderToNodeOpts struct {
|
|
|
|
OmitEmpty bool
|
|
|
|
TagName string
|
|
|
|
AllowSliceAsStruct bool
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
// EncodeToNode converts an element to a node.
|
2020-05-11 10:06:07 +00:00
|
|
|
// element -> nodes.
|
2019-11-28 20:56:04 +00:00
|
|
|
func EncodeToNode(element interface{}, rootName string, opts EncoderToNodeOpts) (*Node, error) {
|
2018-12-04 13:24:04 +00:00
|
|
|
rValue := reflect.ValueOf(element)
|
2019-06-21 08:08:04 +00:00
|
|
|
node := &Node{Name: rootName}
|
2018-12-04 13:24:04 +00:00
|
|
|
|
2019-11-28 20:56:04 +00:00
|
|
|
encoder := encoderToNode{EncoderToNodeOpts: opts}
|
2019-06-17 09:48:05 +00:00
|
|
|
|
|
|
|
err := encoder.setNodeValue(node, rValue)
|
2018-12-04 13:24:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return node, nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
type encoderToNode struct {
|
2019-11-28 20:56:04 +00:00
|
|
|
EncoderToNodeOpts
|
2019-06-17 09:48:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e encoderToNode) setNodeValue(node *Node, rValue reflect.Value) error {
|
2018-12-04 13:24:04 +00:00
|
|
|
switch rValue.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
node.Value = rValue.String()
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
node.Value = strconv.FormatInt(rValue.Int(), 10)
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
node.Value = strconv.FormatUint(rValue.Uint(), 10)
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
node.Value = strconv.FormatFloat(rValue.Float(), 'f', 6, 64)
|
|
|
|
case reflect.Bool:
|
|
|
|
node.Value = strconv.FormatBool(rValue.Bool())
|
|
|
|
case reflect.Struct:
|
2019-06-17 09:48:05 +00:00
|
|
|
return e.setStructValue(node, rValue)
|
2018-12-04 13:24:04 +00:00
|
|
|
case reflect.Ptr:
|
2019-06-17 09:48:05 +00:00
|
|
|
return e.setNodeValue(node, rValue.Elem())
|
2018-12-04 13:24:04 +00:00
|
|
|
case reflect.Map:
|
2019-06-17 09:48:05 +00:00
|
|
|
return e.setMapValue(node, rValue)
|
2018-12-04 13:24:04 +00:00
|
|
|
case reflect.Slice:
|
2019-06-17 09:48:05 +00:00
|
|
|
return e.setSliceValue(node, rValue)
|
2018-12-04 13:24:04 +00:00
|
|
|
default:
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
func (e encoderToNode) setStructValue(node *Node, rValue reflect.Value) error {
|
2018-12-04 13:24:04 +00:00
|
|
|
rType := rValue.Type()
|
|
|
|
|
|
|
|
for i := 0; i < rValue.NumField(); i++ {
|
|
|
|
field := rType.Field(i)
|
|
|
|
fieldValue := rValue.Field(i)
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if !IsExported(field) {
|
2018-12-04 13:24:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:56:04 +00:00
|
|
|
if field.Tag.Get(e.TagName) == "-" {
|
2018-12-04 13:24:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := isSupportedType(field); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if e.isSkippedField(field, fieldValue) {
|
2018-12-04 13:24:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeName := field.Name
|
2019-11-28 20:56:04 +00:00
|
|
|
if e.AllowSliceAsStruct && field.Type.Kind() == reflect.Slice && len(field.Tag.Get(TagLabelSliceAsStruct)) != 0 {
|
2018-12-04 13:24:04 +00:00
|
|
|
nodeName = field.Tag.Get(TagLabelSliceAsStruct)
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if field.Anonymous {
|
|
|
|
if err := e.setNodeValue(node, fieldValue); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
2018-12-04 13:24:04 +00:00
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
child := &Node{Name: nodeName, FieldName: field.Name, Description: field.Tag.Get(TagDescription)}
|
|
|
|
|
|
|
|
if err := e.setNodeValue(child, fieldValue); err != nil {
|
2018-12-04 13:24:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if field.Type.Kind() == reflect.Ptr {
|
|
|
|
if field.Type.Elem().Kind() != reflect.Struct && fieldValue.IsNil() {
|
2018-12-04 13:24:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if field.Type.Elem().Kind() == reflect.Struct && len(child.Children) == 0 {
|
2019-11-28 20:56:04 +00:00
|
|
|
if field.Tag.Get(e.TagName) != TagLabelAllowEmpty {
|
2019-06-17 09:48:05 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
child.Value = "true"
|
|
|
|
}
|
2018-12-04 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
node.Children = append(node.Children, child)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
func (e encoderToNode) setMapValue(node *Node, rValue reflect.Value) error {
|
2020-07-12 10:56:22 +00:00
|
|
|
if rValue.Type().Elem().Kind() == reflect.Interface {
|
|
|
|
node.RawValue = rValue.Interface()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-12-04 13:24:04 +00:00
|
|
|
for _, key := range rValue.MapKeys() {
|
|
|
|
child := &Node{Name: key.String(), FieldName: key.String()}
|
|
|
|
node.Children = append(node.Children, child)
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
if err := e.setNodeValue(child, rValue.MapIndex(key)); err != nil {
|
2018-12-04 13:24:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
func (e encoderToNode) setSliceValue(node *Node, rValue reflect.Value) error {
|
2018-12-04 13:24:04 +00:00
|
|
|
// label-slice-as-struct
|
|
|
|
if rValue.Type().Elem().Kind() == reflect.Struct && !strings.EqualFold(node.Name, node.FieldName) {
|
|
|
|
if rValue.Len() > 1 {
|
|
|
|
return fmt.Errorf("node %s has too many slice entries: %d", node.Name, rValue.Len())
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
return e.setNodeValue(node, rValue.Index(0))
|
|
|
|
}
|
|
|
|
|
|
|
|
if rValue.Type().Elem().Kind() == reflect.Struct ||
|
|
|
|
rValue.Type().Elem().Kind() == reflect.Ptr && rValue.Type().Elem().Elem().Kind() == reflect.Struct {
|
|
|
|
for i := 0; i < rValue.Len(); i++ {
|
|
|
|
child := &Node{Name: "[" + strconv.Itoa(i) + "]"}
|
|
|
|
|
|
|
|
eValue := rValue.Index(i)
|
|
|
|
|
|
|
|
err := e.setNodeValue(child, eValue)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Children = append(node.Children, child)
|
2018-12-04 13:24:04 +00:00
|
|
|
}
|
2019-06-17 09:48:05 +00:00
|
|
|
|
|
|
|
return nil
|
2018-12-04 13:24:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var values []string
|
|
|
|
|
|
|
|
for i := 0; i < rValue.Len(); i++ {
|
|
|
|
eValue := rValue.Index(i)
|
|
|
|
|
|
|
|
switch eValue.Kind() {
|
|
|
|
case reflect.String:
|
|
|
|
values = append(values, eValue.String())
|
|
|
|
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
|
|
|
|
values = append(values, strconv.FormatInt(eValue.Int(), 10))
|
|
|
|
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
|
|
|
|
values = append(values, strconv.FormatUint(eValue.Uint(), 10))
|
|
|
|
case reflect.Float32, reflect.Float64:
|
|
|
|
values = append(values, strconv.FormatFloat(eValue.Float(), 'f', 6, 64))
|
|
|
|
case reflect.Bool:
|
|
|
|
values = append(values, strconv.FormatBool(eValue.Bool()))
|
|
|
|
default:
|
|
|
|
// noop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
node.Value = strings.Join(values, ", ")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-06-17 09:48:05 +00:00
|
|
|
func (e encoderToNode) isSkippedField(field reflect.StructField, fieldValue reflect.Value) bool {
|
2019-11-28 20:56:04 +00:00
|
|
|
if e.OmitEmpty && field.Type.Kind() == reflect.String && fieldValue.Len() == 0 {
|
2018-12-04 13:24:04 +00:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if field.Type.Kind() == reflect.Ptr && field.Type.Elem().Kind() == reflect.Struct && fieldValue.IsNil() {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-11-28 20:56:04 +00:00
|
|
|
if e.OmitEmpty && (field.Type.Kind() == reflect.Slice) &&
|
2019-06-17 09:48:05 +00:00
|
|
|
(fieldValue.IsNil() || fieldValue.Len() == 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (field.Type.Kind() == reflect.Map) &&
|
2018-12-04 13:24:04 +00:00
|
|
|
(fieldValue.IsNil() || fieldValue.Len() == 0) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|