// Autogenerated by Thrift Compiler (0.11.0)
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING

package aggregation_validator

import (
	"bytes"
	"context"
	"fmt"
	"reflect"

	"git.apache.org/thrift.git/lib/go/thrift"
)

// (needed to ensure safety because of naive import list construction.)
var _ = thrift.ZERO
var _ = fmt.Printf
var _ = context.Background
var _ = reflect.DeepEqual
var _ = bytes.Equal

// Attributes:
//  - Ok
//  - TraceCount
type ValidateTraceResponse struct {
	Ok         bool  `thrift:"ok,1,required" db:"ok" json:"ok"`
	TraceCount int64 `thrift:"traceCount,2,required" db:"traceCount" json:"traceCount"`
}

func NewValidateTraceResponse() *ValidateTraceResponse {
	return &ValidateTraceResponse{}
}

func (p *ValidateTraceResponse) GetOk() bool {
	return p.Ok
}

func (p *ValidateTraceResponse) GetTraceCount() int64 {
	return p.TraceCount
}
func (p *ValidateTraceResponse) Read(iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	var issetOk bool = false
	var issetTraceCount bool = false

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 1:
			if fieldTypeId == thrift.BOOL {
				if err := p.ReadField1(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
			issetOk = true
		case 2:
			if fieldTypeId == thrift.I64 {
				if err := p.ReadField2(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
			issetTraceCount = true
		default:
			if err := iprot.Skip(fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	if !issetOk {
		return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("Required field Ok is not set"))
	}
	if !issetTraceCount {
		return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("Required field TraceCount is not set"))
	}
	return nil
}

func (p *ValidateTraceResponse) ReadField1(iprot thrift.TProtocol) error {
	if v, err := iprot.ReadBool(); err != nil {
		return thrift.PrependError("error reading field 1: ", err)
	} else {
		p.Ok = v
	}
	return nil
}

func (p *ValidateTraceResponse) ReadField2(iprot thrift.TProtocol) error {
	if v, err := iprot.ReadI64(); err != nil {
		return thrift.PrependError("error reading field 2: ", err)
	} else {
		p.TraceCount = v
	}
	return nil
}

func (p *ValidateTraceResponse) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("ValidateTraceResponse"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField1(oprot); err != nil {
			return err
		}
		if err := p.writeField2(oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *ValidateTraceResponse) writeField1(oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("ok", thrift.BOOL, 1); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:ok: ", p), err)
	}
	if err := oprot.WriteBool(bool(p.Ok)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.ok (1) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 1:ok: ", p), err)
	}
	return err
}

func (p *ValidateTraceResponse) writeField2(oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("traceCount", thrift.I64, 2); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 2:traceCount: ", p), err)
	}
	if err := oprot.WriteI64(int64(p.TraceCount)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.traceCount (2) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 2:traceCount: ", p), err)
	}
	return err
}

func (p *ValidateTraceResponse) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("ValidateTraceResponse(%+v)", *p)
}

type AggregationValidator interface {
	// Parameters:
	//  - TraceId
	ValidateTrace(ctx context.Context, traceId string) (r *ValidateTraceResponse, err error)
}

type AggregationValidatorClient struct {
	c thrift.TClient
}

// Deprecated: Use NewAggregationValidator instead
func NewAggregationValidatorClientFactory(t thrift.TTransport, f thrift.TProtocolFactory) *AggregationValidatorClient {
	return &AggregationValidatorClient{
		c: thrift.NewTStandardClient(f.GetProtocol(t), f.GetProtocol(t)),
	}
}

// Deprecated: Use NewAggregationValidator instead
func NewAggregationValidatorClientProtocol(t thrift.TTransport, iprot thrift.TProtocol, oprot thrift.TProtocol) *AggregationValidatorClient {
	return &AggregationValidatorClient{
		c: thrift.NewTStandardClient(iprot, oprot),
	}
}

func NewAggregationValidatorClient(c thrift.TClient) *AggregationValidatorClient {
	return &AggregationValidatorClient{
		c: c,
	}
}

// Parameters:
//  - TraceId
func (p *AggregationValidatorClient) ValidateTrace(ctx context.Context, traceId string) (r *ValidateTraceResponse, err error) {
	var _args0 AggregationValidatorValidateTraceArgs
	_args0.TraceId = traceId
	var _result1 AggregationValidatorValidateTraceResult
	if err = p.c.Call(ctx, "validateTrace", &_args0, &_result1); err != nil {
		return
	}
	return _result1.GetSuccess(), nil
}

type AggregationValidatorProcessor struct {
	processorMap map[string]thrift.TProcessorFunction
	handler      AggregationValidator
}

func (p *AggregationValidatorProcessor) AddToProcessorMap(key string, processor thrift.TProcessorFunction) {
	p.processorMap[key] = processor
}

func (p *AggregationValidatorProcessor) GetProcessorFunction(key string) (processor thrift.TProcessorFunction, ok bool) {
	processor, ok = p.processorMap[key]
	return processor, ok
}

func (p *AggregationValidatorProcessor) ProcessorMap() map[string]thrift.TProcessorFunction {
	return p.processorMap
}

func NewAggregationValidatorProcessor(handler AggregationValidator) *AggregationValidatorProcessor {

	self2 := &AggregationValidatorProcessor{handler: handler, processorMap: make(map[string]thrift.TProcessorFunction)}
	self2.processorMap["validateTrace"] = &aggregationValidatorProcessorValidateTrace{handler: handler}
	return self2
}

func (p *AggregationValidatorProcessor) Process(ctx context.Context, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
	name, _, seqId, err := iprot.ReadMessageBegin()
	if err != nil {
		return false, err
	}
	if processor, ok := p.GetProcessorFunction(name); ok {
		return processor.Process(ctx, seqId, iprot, oprot)
	}
	iprot.Skip(thrift.STRUCT)
	iprot.ReadMessageEnd()
	x3 := thrift.NewTApplicationException(thrift.UNKNOWN_METHOD, "Unknown function "+name)
	oprot.WriteMessageBegin(name, thrift.EXCEPTION, seqId)
	x3.Write(oprot)
	oprot.WriteMessageEnd()
	oprot.Flush()
	return false, x3

}

type aggregationValidatorProcessorValidateTrace struct {
	handler AggregationValidator
}

func (p *aggregationValidatorProcessorValidateTrace) Process(ctx context.Context, seqId int32, iprot, oprot thrift.TProtocol) (success bool, err thrift.TException) {
	args := AggregationValidatorValidateTraceArgs{}
	if err = args.Read(iprot); err != nil {
		iprot.ReadMessageEnd()
		x := thrift.NewTApplicationException(thrift.PROTOCOL_ERROR, err.Error())
		oprot.WriteMessageBegin("validateTrace", thrift.EXCEPTION, seqId)
		x.Write(oprot)
		oprot.WriteMessageEnd()
		oprot.Flush()
		return false, err
	}

	iprot.ReadMessageEnd()
	result := AggregationValidatorValidateTraceResult{}
	var retval *ValidateTraceResponse
	var err2 error
	if retval, err2 = p.handler.ValidateTrace(ctx, args.TraceId); err2 != nil {
		x := thrift.NewTApplicationException(thrift.INTERNAL_ERROR, "Internal error processing validateTrace: "+err2.Error())
		oprot.WriteMessageBegin("validateTrace", thrift.EXCEPTION, seqId)
		x.Write(oprot)
		oprot.WriteMessageEnd()
		oprot.Flush()
		return true, err2
	} else {
		result.Success = retval
	}
	if err2 = oprot.WriteMessageBegin("validateTrace", thrift.REPLY, seqId); err2 != nil {
		err = err2
	}
	if err2 = result.Write(oprot); err == nil && err2 != nil {
		err = err2
	}
	if err2 = oprot.WriteMessageEnd(); err == nil && err2 != nil {
		err = err2
	}
	if err2 = oprot.Flush(); err == nil && err2 != nil {
		err = err2
	}
	if err != nil {
		return
	}
	return true, err
}

// HELPER FUNCTIONS AND STRUCTURES

// Attributes:
//  - TraceId
type AggregationValidatorValidateTraceArgs struct {
	TraceId string `thrift:"traceId,1,required" db:"traceId" json:"traceId"`
}

func NewAggregationValidatorValidateTraceArgs() *AggregationValidatorValidateTraceArgs {
	return &AggregationValidatorValidateTraceArgs{}
}

func (p *AggregationValidatorValidateTraceArgs) GetTraceId() string {
	return p.TraceId
}
func (p *AggregationValidatorValidateTraceArgs) Read(iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	var issetTraceId bool = false

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 1:
			if fieldTypeId == thrift.STRING {
				if err := p.ReadField1(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
			issetTraceId = true
		default:
			if err := iprot.Skip(fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	if !issetTraceId {
		return thrift.NewTProtocolExceptionWithType(thrift.INVALID_DATA, fmt.Errorf("Required field TraceId is not set"))
	}
	return nil
}

func (p *AggregationValidatorValidateTraceArgs) ReadField1(iprot thrift.TProtocol) error {
	if v, err := iprot.ReadString(); err != nil {
		return thrift.PrependError("error reading field 1: ", err)
	} else {
		p.TraceId = v
	}
	return nil
}

func (p *AggregationValidatorValidateTraceArgs) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("validateTrace_args"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField1(oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *AggregationValidatorValidateTraceArgs) writeField1(oprot thrift.TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("traceId", thrift.STRING, 1); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field begin error 1:traceId: ", p), err)
	}
	if err := oprot.WriteString(string(p.TraceId)); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T.traceId (1) field write error: ", p), err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write field end error 1:traceId: ", p), err)
	}
	return err
}

func (p *AggregationValidatorValidateTraceArgs) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("AggregationValidatorValidateTraceArgs(%+v)", *p)
}

// Attributes:
//  - Success
type AggregationValidatorValidateTraceResult struct {
	Success *ValidateTraceResponse `thrift:"success,0" db:"success" json:"success,omitempty"`
}

func NewAggregationValidatorValidateTraceResult() *AggregationValidatorValidateTraceResult {
	return &AggregationValidatorValidateTraceResult{}
}

var AggregationValidatorValidateTraceResult_Success_DEFAULT *ValidateTraceResponse

func (p *AggregationValidatorValidateTraceResult) GetSuccess() *ValidateTraceResponse {
	if !p.IsSetSuccess() {
		return AggregationValidatorValidateTraceResult_Success_DEFAULT
	}
	return p.Success
}
func (p *AggregationValidatorValidateTraceResult) IsSetSuccess() bool {
	return p.Success != nil
}

func (p *AggregationValidatorValidateTraceResult) Read(iprot thrift.TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read error: ", p), err)
	}

	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", p, fieldId), err)
		}
		if fieldTypeId == thrift.STOP {
			break
		}
		switch fieldId {
		case 0:
			if fieldTypeId == thrift.STRUCT {
				if err := p.ReadField0(iprot); err != nil {
					return err
				}
			} else {
				if err := iprot.Skip(fieldTypeId); err != nil {
					return err
				}
			}
		default:
			if err := iprot.Skip(fieldTypeId); err != nil {
				return err
			}
		}
		if err := iprot.ReadFieldEnd(); err != nil {
			return err
		}
	}
	if err := iprot.ReadStructEnd(); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", p), err)
	}
	return nil
}

func (p *AggregationValidatorValidateTraceResult) ReadField0(iprot thrift.TProtocol) error {
	p.Success = &ValidateTraceResponse{}
	if err := p.Success.Read(iprot); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T error reading struct: ", p.Success), err)
	}
	return nil
}

func (p *AggregationValidatorValidateTraceResult) Write(oprot thrift.TProtocol) error {
	if err := oprot.WriteStructBegin("validateTrace_result"); err != nil {
		return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", p), err)
	}
	if p != nil {
		if err := p.writeField0(oprot); err != nil {
			return err
		}
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return thrift.PrependError("write field stop error: ", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return thrift.PrependError("write struct stop error: ", err)
	}
	return nil
}

func (p *AggregationValidatorValidateTraceResult) writeField0(oprot thrift.TProtocol) (err error) {
	if p.IsSetSuccess() {
		if err := oprot.WriteFieldBegin("success", thrift.STRUCT, 0); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T write field begin error 0:success: ", p), err)
		}
		if err := p.Success.Write(oprot); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T error writing struct: ", p.Success), err)
		}
		if err := oprot.WriteFieldEnd(); err != nil {
			return thrift.PrependError(fmt.Sprintf("%T write field end error 0:success: ", p), err)
		}
	}
	return err
}

func (p *AggregationValidatorValidateTraceResult) String() string {
	if p == nil {
		return "<nil>"
	}
	return fmt.Sprintf("AggregationValidatorValidateTraceResult(%+v)", *p)
}
