github.com/urso/go-structform@v0.0.2/visitor.go (about)

     1  package structform
     2  
     3  // Visitor interface for iterating some structured input
     4  type Visitor interface {
     5  	ObjectVisitor
     6  	ArrayVisitor
     7  	ValueVisitor
     8  }
     9  
    10  type ExtVisitor interface {
    11  	Visitor
    12  	ArrayValueVisitor
    13  	ObjectValueVisitor
    14  	StringRefVisitor
    15  }
    16  
    17  //go:generate stringer -type=BaseType
    18  type BaseType uint8
    19  
    20  const (
    21  	AnyType BaseType = iota
    22  	ByteType
    23  	StringType
    24  	BoolType
    25  	ZeroType
    26  	IntType
    27  	Int8Type
    28  	Int16Type
    29  	Int32Type
    30  	Int64Type
    31  	UintType
    32  	Uint8Type
    33  	Uint16Type
    34  	Uint32Type
    35  	Uint64Type
    36  	Float32Type
    37  	Float64Type
    38  )
    39  
    40  // ObjectVisitor iterates all fields in a dictionary like structure
    41  type ObjectVisitor interface {
    42  	OnObjectStart(len int, baseType BaseType) error
    43  	OnObjectFinished() error
    44  	OnKey(s string) error
    45  }
    46  
    47  // ArrayVisitor iterates all entries in a list/array like structure
    48  type ArrayVisitor interface {
    49  	OnArrayStart(len int, baseType BaseType) error
    50  	OnArrayFinished() error
    51  }
    52  
    53  // ValueVisitor reports actual values in a structure being iterated
    54  type ValueVisitor interface {
    55  	OnNil() error
    56  
    57  	OnBool(b bool) error
    58  
    59  	OnString(s string) error
    60  
    61  	// int
    62  	OnInt8(i int8) error
    63  	OnInt16(i int16) error
    64  	OnInt32(i int32) error
    65  	OnInt64(i int64) error
    66  	OnInt(i int) error
    67  
    68  	// uint
    69  	OnByte(b byte) error
    70  	OnUint8(u uint8) error
    71  	OnUint16(u uint16) error
    72  	OnUint32(u uint32) error
    73  	OnUint64(u uint64) error
    74  	OnUint(u uint) error
    75  
    76  	// float
    77  	OnFloat32(f float32) error
    78  	OnFloat64(f float64) error
    79  }
    80  
    81  // ArrayValueVisitor passes arrays with known type. Implementation
    82  // of ArrayValueVisitor is optional.
    83  type ArrayValueVisitor interface {
    84  	OnBoolArray([]bool) error
    85  
    86  	OnStringArray([]string) error
    87  
    88  	// int
    89  	OnInt8Array([]int8) error
    90  	OnInt16Array([]int16) error
    91  	OnInt32Array([]int32) error
    92  	OnInt64Array([]int64) error
    93  	OnIntArray([]int) error
    94  
    95  	// uint
    96  	OnBytes([]byte) error
    97  	OnUint8Array([]uint8) error
    98  	OnUint16Array([]uint16) error
    99  	OnUint32Array([]uint32) error
   100  	OnUint64Array([]uint64) error
   101  	OnUintArray([]uint) error
   102  
   103  	// float
   104  	OnFloat32Array([]float32) error
   105  	OnFloat64Array([]float64) error
   106  }
   107  
   108  // ObjectValueVisitor passes map[string]T. Implementation
   109  // of ObjectValueVisitor is optional.
   110  type ObjectValueVisitor interface {
   111  	OnBoolObject(map[string]bool) error
   112  
   113  	OnStringObject(map[string]string) error
   114  
   115  	// int
   116  	OnInt8Object(map[string]int8) error
   117  	OnInt16Object(map[string]int16) error
   118  	OnInt32Object(map[string]int32) error
   119  	OnInt64Object(map[string]int64) error
   120  	OnIntObject(map[string]int) error
   121  
   122  	// uint
   123  	OnUint8Object(map[string]uint8) error
   124  	OnUint16Object(map[string]uint16) error
   125  	OnUint32Object(map[string]uint32) error
   126  	OnUint64Object(map[string]uint64) error
   127  	OnUintObject(map[string]uint) error
   128  
   129  	// float
   130  	OnFloat32Object(map[string]float32) error
   131  	OnFloat64Object(map[string]float64) error
   132  }
   133  
   134  // StringRefVisitor handles strings by reference into a byte string.
   135  // The reference must be processed immediately, as the string passed
   136  // might get overwritten after the callback returns.
   137  type StringRefVisitor interface {
   138  	OnStringRef(s []byte) error
   139  	OnKeyRef(s []byte) error
   140  }
   141  
   142  type extVisitor struct {
   143  	Visitor
   144  	ObjectValueVisitor
   145  	ArrayValueVisitor
   146  	StringRefVisitor
   147  }
   148  
   149  func EnsureExtVisitor(v Visitor) ExtVisitor {
   150  	if ev, ok := v.(ExtVisitor); ok {
   151  		return ev
   152  	}
   153  
   154  	e := &extVisitor{
   155  		Visitor: v,
   156  	}
   157  	if ov, ok := v.(ObjectValueVisitor); ok {
   158  		e.ObjectValueVisitor = ov
   159  	} else {
   160  		e.ObjectValueVisitor = extObjVisitor{v}
   161  	}
   162  	if av, ok := v.(ArrayValueVisitor); ok {
   163  		e.ArrayValueVisitor = av
   164  	} else {
   165  		e.ArrayValueVisitor = extArrVisitor{v}
   166  	}
   167  	e.StringRefVisitor = MakeStringRefVisitor(v)
   168  
   169  	return e
   170  }