/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package thrift

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

/* THE FOLLOWING THRIFT FILE WAS USED TO CREATE THIS

enum TestEnum {
	FIRST = 1,
	SECOND = 2,
	THIRD = 3,
	FOURTH = 4,
}

struct TestStruct {
	1: bool on,
	2: byte b,
	3: i16 int16,
	4: i32 int32,
	5: i64 int64,
	6: double d,
	7: string st,
	8: binary bin,
	9: map<string, string> stringMap,
	10: list<string> stringList,
	11: set<string> stringSet,
	12: TestEnum e,
}
*/

import (
	"fmt"
)

// (needed to ensure safety because of naive import list construction.)
var _ = ZERO
var _ = fmt.Printf

var GoUnusedProtection__ int

type TestEnum int64

const (
	TestEnum_FIRST  TestEnum = 1
	TestEnum_SECOND TestEnum = 2
	TestEnum_THIRD  TestEnum = 3
	TestEnum_FOURTH TestEnum = 4
)

func (p TestEnum) String() string {
	switch p {
	case TestEnum_FIRST:
		return "TestEnum_FIRST"
	case TestEnum_SECOND:
		return "TestEnum_SECOND"
	case TestEnum_THIRD:
		return "TestEnum_THIRD"
	case TestEnum_FOURTH:
		return "TestEnum_FOURTH"
	}
	return "<UNSET>"
}

func TestEnumFromString(s string) (TestEnum, error) {
	switch s {
	case "TestEnum_FIRST":
		return TestEnum_FIRST, nil
	case "TestEnum_SECOND":
		return TestEnum_SECOND, nil
	case "TestEnum_THIRD":
		return TestEnum_THIRD, nil
	case "TestEnum_FOURTH":
		return TestEnum_FOURTH, nil
	}
	return TestEnum(0), fmt.Errorf("not a valid TestEnum string")
}

func TestEnumPtr(v TestEnum) *TestEnum { return &v }

type TestStruct struct {
	On         bool              `thrift:"on,1"`
	B          int8              `thrift:"b,2"`
	Int16      int16             `thrift:"int16,3"`
	Int32      int32             `thrift:"int32,4"`
	Int64      int64             `thrift:"int64,5"`
	D          float64           `thrift:"d,6"`
	St         string            `thrift:"st,7"`
	Bin        []byte            `thrift:"bin,8"`
	StringMap  map[string]string `thrift:"stringMap,9"`
	StringList []string          `thrift:"stringList,10"`
	StringSet  map[string]bool   `thrift:"stringSet,11"`
	E          TestEnum          `thrift:"e,12"`
}

func NewTestStruct() *TestStruct {
	rval := &TestStruct{}
	return rval
}

func (p *TestStruct) Read(iprot TProtocol) error {
	if _, err := iprot.ReadStructBegin(); err != nil {
		return fmt.Errorf("%T read error: %s", p, err)
	}
	for {
		_, fieldTypeId, fieldId, err := iprot.ReadFieldBegin()
		if err != nil {
			return fmt.Errorf("%T field %d read error: %s", p, fieldId, err)
		}
		if fieldTypeId == STOP {
			break
		}
		switch fieldId {
		case 1:
			if err := p.readField1(iprot); err != nil {
				return err
			}
		case 2:
			if err := p.readField2(iprot); err != nil {
				return err
			}
		case 3:
			if err := p.readField3(iprot); err != nil {
				return err
			}
		case 4:
			if err := p.readField4(iprot); err != nil {
				return err
			}
		case 5:
			if err := p.readField5(iprot); err != nil {
				return err
			}
		case 6:
			if err := p.readField6(iprot); err != nil {
				return err
			}
		case 7:
			if err := p.readField7(iprot); err != nil {
				return err
			}
		case 8:
			if err := p.readField8(iprot); err != nil {
				return err
			}
		case 9:
			if err := p.readField9(iprot); err != nil {
				return err
			}
		case 10:
			if err := p.readField10(iprot); err != nil {
				return err
			}
		case 11:
			if err := p.readField11(iprot); err != nil {
				return err
			}
		case 12:
			if err := p.readField12(iprot); 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 fmt.Errorf("%T read struct end error: %s", p, err)
	}
	return nil
}

func (p *TestStruct) readField1(iprot TProtocol) error {
	if v, err := iprot.ReadBool(); err != nil {
		return fmt.Errorf("error reading field 1: %s", err)
	} else {
		p.On = v
	}
	return nil
}

func (p *TestStruct) readField2(iprot TProtocol) error {
	if v, err := iprot.ReadByte(); err != nil {
		return fmt.Errorf("error reading field 2: %s", err)
	} else {
		temp := int8(v)
		p.B = temp
	}
	return nil
}

func (p *TestStruct) readField3(iprot TProtocol) error {
	if v, err := iprot.ReadI16(); err != nil {
		return fmt.Errorf("error reading field 3: %s", err)
	} else {
		p.Int16 = v
	}
	return nil
}

func (p *TestStruct) readField4(iprot TProtocol) error {
	if v, err := iprot.ReadI32(); err != nil {
		return fmt.Errorf("error reading field 4: %s", err)
	} else {
		p.Int32 = v
	}
	return nil
}

func (p *TestStruct) readField5(iprot TProtocol) error {
	if v, err := iprot.ReadI64(); err != nil {
		return fmt.Errorf("error reading field 5: %s", err)
	} else {
		p.Int64 = v
	}
	return nil
}

func (p *TestStruct) readField6(iprot TProtocol) error {
	if v, err := iprot.ReadDouble(); err != nil {
		return fmt.Errorf("error reading field 6: %s", err)
	} else {
		p.D = v
	}
	return nil
}

func (p *TestStruct) readField7(iprot TProtocol) error {
	if v, err := iprot.ReadString(); err != nil {
		return fmt.Errorf("error reading field 7: %s", err)
	} else {
		p.St = v
	}
	return nil
}

func (p *TestStruct) readField8(iprot TProtocol) error {
	if v, err := iprot.ReadBinary(); err != nil {
		return fmt.Errorf("error reading field 8: %s", err)
	} else {
		p.Bin = v
	}
	return nil
}

func (p *TestStruct) readField9(iprot TProtocol) error {
	_, _, size, err := iprot.ReadMapBegin()
	if err != nil {
		return fmt.Errorf("error reading map begin: %s")
	}
	tMap := make(map[string]string, size)
	p.StringMap = tMap
	for i := 0; i < size; i++ {
		var _key0 string
		if v, err := iprot.ReadString(); err != nil {
			return fmt.Errorf("error reading field 0: %s", err)
		} else {
			_key0 = v
		}
		var _val1 string
		if v, err := iprot.ReadString(); err != nil {
			return fmt.Errorf("error reading field 0: %s", err)
		} else {
			_val1 = v
		}
		p.StringMap[_key0] = _val1
	}
	if err := iprot.ReadMapEnd(); err != nil {
		return fmt.Errorf("error reading map end: %s")
	}
	return nil
}

func (p *TestStruct) readField10(iprot TProtocol) error {
	_, size, err := iprot.ReadListBegin()
	if err != nil {
		return fmt.Errorf("error reading list begin: %s")
	}
	tSlice := make([]string, 0, size)
	p.StringList = tSlice
	for i := 0; i < size; i++ {
		var _elem2 string
		if v, err := iprot.ReadString(); err != nil {
			return fmt.Errorf("error reading field 0: %s", err)
		} else {
			_elem2 = v
		}
		p.StringList = append(p.StringList, _elem2)
	}
	if err := iprot.ReadListEnd(); err != nil {
		return fmt.Errorf("error reading list end: %s")
	}
	return nil
}

func (p *TestStruct) readField11(iprot TProtocol) error {
	_, size, err := iprot.ReadSetBegin()
	if err != nil {
		return fmt.Errorf("error reading set begin: %s")
	}
	tSet := make(map[string]bool, size)
	p.StringSet = tSet
	for i := 0; i < size; i++ {
		var _elem3 string
		if v, err := iprot.ReadString(); err != nil {
			return fmt.Errorf("error reading field 0: %s", err)
		} else {
			_elem3 = v
		}
		p.StringSet[_elem3] = true
	}
	if err := iprot.ReadSetEnd(); err != nil {
		return fmt.Errorf("error reading set end: %s")
	}
	return nil
}

func (p *TestStruct) readField12(iprot TProtocol) error {
	if v, err := iprot.ReadI32(); err != nil {
		return fmt.Errorf("error reading field 12: %s", err)
	} else {
		temp := TestEnum(v)
		p.E = temp
	}
	return nil
}

func (p *TestStruct) Write(oprot TProtocol) error {
	if err := oprot.WriteStructBegin("TestStruct"); err != nil {
		return fmt.Errorf("%T write struct begin error: %s", p, err)
	}
	if err := p.writeField1(oprot); err != nil {
		return err
	}
	if err := p.writeField2(oprot); err != nil {
		return err
	}
	if err := p.writeField3(oprot); err != nil {
		return err
	}
	if err := p.writeField4(oprot); err != nil {
		return err
	}
	if err := p.writeField5(oprot); err != nil {
		return err
	}
	if err := p.writeField6(oprot); err != nil {
		return err
	}
	if err := p.writeField7(oprot); err != nil {
		return err
	}
	if err := p.writeField8(oprot); err != nil {
		return err
	}
	if err := p.writeField9(oprot); err != nil {
		return err
	}
	if err := p.writeField10(oprot); err != nil {
		return err
	}
	if err := p.writeField11(oprot); err != nil {
		return err
	}
	if err := p.writeField12(oprot); err != nil {
		return err
	}
	if err := oprot.WriteFieldStop(); err != nil {
		return fmt.Errorf("write field stop error: %s", err)
	}
	if err := oprot.WriteStructEnd(); err != nil {
		return fmt.Errorf("write struct stop error: %s", err)
	}
	return nil
}

func (p *TestStruct) writeField1(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("on", BOOL, 1); err != nil {
		return fmt.Errorf("%T write field begin error 1:on: %s", p, err)
	}
	if err := oprot.WriteBool(bool(p.On)); err != nil {
		return fmt.Errorf("%T.on (1) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 1:on: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField2(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("b", BYTE, 2); err != nil {
		return fmt.Errorf("%T write field begin error 2:b: %s", p, err)
	}
	if err := oprot.WriteByte(byte(p.B)); err != nil {
		return fmt.Errorf("%T.b (2) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 2:b: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField3(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("int16", I16, 3); err != nil {
		return fmt.Errorf("%T write field begin error 3:int16: %s", p, err)
	}
	if err := oprot.WriteI16(int16(p.Int16)); err != nil {
		return fmt.Errorf("%T.int16 (3) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 3:int16: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField4(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("int32", I32, 4); err != nil {
		return fmt.Errorf("%T write field begin error 4:int32: %s", p, err)
	}
	if err := oprot.WriteI32(int32(p.Int32)); err != nil {
		return fmt.Errorf("%T.int32 (4) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 4:int32: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField5(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("int64", I64, 5); err != nil {
		return fmt.Errorf("%T write field begin error 5:int64: %s", p, err)
	}
	if err := oprot.WriteI64(int64(p.Int64)); err != nil {
		return fmt.Errorf("%T.int64 (5) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 5:int64: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField6(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("d", DOUBLE, 6); err != nil {
		return fmt.Errorf("%T write field begin error 6:d: %s", p, err)
	}
	if err := oprot.WriteDouble(float64(p.D)); err != nil {
		return fmt.Errorf("%T.d (6) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 6:d: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField7(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("st", STRING, 7); err != nil {
		return fmt.Errorf("%T write field begin error 7:st: %s", p, err)
	}
	if err := oprot.WriteString(string(p.St)); err != nil {
		return fmt.Errorf("%T.st (7) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 7:st: %s", p, err)
	}
	return err
}

func (p *TestStruct) writeField8(oprot TProtocol) (err error) {
	if p.Bin != nil {
		if err := oprot.WriteFieldBegin("bin", STRING, 8); err != nil {
			return fmt.Errorf("%T write field begin error 8:bin: %s", p, err)
		}
		if err := oprot.WriteBinary(p.Bin); err != nil {
			return fmt.Errorf("%T.bin (8) field write error: %s", p, err)
		}
		if err := oprot.WriteFieldEnd(); err != nil {
			return fmt.Errorf("%T write field end error 8:bin: %s", p, err)
		}
	}
	return err
}

func (p *TestStruct) writeField9(oprot TProtocol) (err error) {
	if p.StringMap != nil {
		if err := oprot.WriteFieldBegin("stringMap", MAP, 9); err != nil {
			return fmt.Errorf("%T write field begin error 9:stringMap: %s", p, err)
		}
		if err := oprot.WriteMapBegin(STRING, STRING, len(p.StringMap)); err != nil {
			return fmt.Errorf("error writing map begin: %s")
		}
		for k, v := range p.StringMap {
			if err := oprot.WriteString(string(k)); err != nil {
				return fmt.Errorf("%T. (0) field write error: %s", p, err)
			}
			if err := oprot.WriteString(string(v)); err != nil {
				return fmt.Errorf("%T. (0) field write error: %s", p, err)
			}
		}
		if err := oprot.WriteMapEnd(); err != nil {
			return fmt.Errorf("error writing map end: %s")
		}
		if err := oprot.WriteFieldEnd(); err != nil {
			return fmt.Errorf("%T write field end error 9:stringMap: %s", p, err)
		}
	}
	return err
}

func (p *TestStruct) writeField10(oprot TProtocol) (err error) {
	if p.StringList != nil {
		if err := oprot.WriteFieldBegin("stringList", LIST, 10); err != nil {
			return fmt.Errorf("%T write field begin error 10:stringList: %s", p, err)
		}
		if err := oprot.WriteListBegin(STRING, len(p.StringList)); err != nil {
			return fmt.Errorf("error writing list begin: %s")
		}
		for _, v := range p.StringList {
			if err := oprot.WriteString(string(v)); err != nil {
				return fmt.Errorf("%T. (0) field write error: %s", p, err)
			}
		}
		if err := oprot.WriteListEnd(); err != nil {
			return fmt.Errorf("error writing list end: %s")
		}
		if err := oprot.WriteFieldEnd(); err != nil {
			return fmt.Errorf("%T write field end error 10:stringList: %s", p, err)
		}
	}
	return err
}

func (p *TestStruct) writeField11(oprot TProtocol) (err error) {
	if p.StringSet != nil {
		if err := oprot.WriteFieldBegin("stringSet", SET, 11); err != nil {
			return fmt.Errorf("%T write field begin error 11:stringSet: %s", p, err)
		}
		if err := oprot.WriteSetBegin(STRING, len(p.StringSet)); err != nil {
			return fmt.Errorf("error writing set begin: %s")
		}
		for v, _ := range p.StringSet {
			if err := oprot.WriteString(string(v)); err != nil {
				return fmt.Errorf("%T. (0) field write error: %s", p, err)
			}
		}
		if err := oprot.WriteSetEnd(); err != nil {
			return fmt.Errorf("error writing set end: %s")
		}
		if err := oprot.WriteFieldEnd(); err != nil {
			return fmt.Errorf("%T write field end error 11:stringSet: %s", p, err)
		}
	}
	return err
}

func (p *TestStruct) writeField12(oprot TProtocol) (err error) {
	if err := oprot.WriteFieldBegin("e", I32, 12); err != nil {
		return fmt.Errorf("%T write field begin error 12:e: %s", p, err)
	}
	if err := oprot.WriteI32(int32(p.E)); err != nil {
		return fmt.Errorf("%T.e (12) field write error: %s", p, err)
	}
	if err := oprot.WriteFieldEnd(); err != nil {
		return fmt.Errorf("%T write field end error 12:e: %s", p, err)
	}
	return err
}

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