// Autogenerated by Thrift for thrift/compiler/test/fixtures/default_values/src/module.thrift
//
// DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
//  @generated

package module

import (
    "fmt"
    "reflect"

    thrift "github.com/facebook/fbthrift/thrift/lib/go/thrift/types"
)

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

type TrivialStruct struct {
    IntValue int32 `thrift:"int_value,1" json:"int_value" db:"int_value"`
}
// Compile time interface enforcer
var _ thrift.Struct = (*TrivialStruct)(nil)

func NewTrivialStruct() *TrivialStruct {
    return (&TrivialStruct{}).setDefaults()
}

func (x *TrivialStruct) GetIntValue() int32 {
    return x.IntValue
}

func (x *TrivialStruct) SetIntValueNonCompat(value int32) *TrivialStruct {
    x.IntValue = value
    return x
}

func (x *TrivialStruct) SetIntValue(value int32) *TrivialStruct {
    x.IntValue = value
    return x
}

func (x *TrivialStruct) writeField1(p thrift.Encoder) error {  // IntValue
    if err := p.WriteFieldBegin("int_value", thrift.I32, 1); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.IntValue
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *TrivialStruct) readField1(p thrift.Decoder) error {  // IntValue
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.IntValue = result
    return nil
}



func (x *TrivialStruct) Write(p thrift.Encoder) error {
    if err := p.WriteStructBegin("TrivialStruct"); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", x), err)
    }

    if err := x.writeField1(p); err != nil {
        return err
    }

    if err := p.WriteFieldStop(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", x), err)
    }

    if err := p.WriteStructEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", x), err)
    }
    return nil
}

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

    for {
        _, wireType, id, err := p.ReadFieldBegin()
        if err != nil {
            return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", x, id), err)
        }

        if wireType == thrift.STOP {
            break;
        }

        var fieldReadErr error
        switch {
        case (id == 1 && wireType == thrift.Type(thrift.I32)):  // int_value
            fieldReadErr = x.readField1(p)
        default:
            fieldReadErr = p.Skip(wireType)
        }

        if fieldReadErr != nil {
            return fieldReadErr
        }

        if err := p.ReadFieldEnd(); err != nil {
            return err
        }
    }

    if err := p.ReadStructEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", x), err)
    }

    return nil
}

func (x *TrivialStruct) String() string {
    return thrift.StructToString(reflect.ValueOf(x))
}

func (x *TrivialStruct) setDefaults() *TrivialStruct {
    return x.
        SetIntValueNonCompat(0)
}

type StructWithNoCustomDefaultValues struct {
    UnqualifiedInteger int32 `thrift:"unqualified_integer,1" json:"unqualified_integer" db:"unqualified_integer"`
    OptionalInteger *int32 `thrift:"optional_integer,2,optional" json:"optional_integer,omitempty" db:"optional_integer"`
    RequiredInteger int32 `thrift:"required_integer,3,required" json:"required_integer" db:"required_integer"`
    UnqualifiedStruct *TrivialStruct `thrift:"unqualified_struct,4" json:"unqualified_struct" db:"unqualified_struct"`
    OptionalStruct *TrivialStruct `thrift:"optional_struct,5,optional" json:"optional_struct,omitempty" db:"optional_struct"`
    RequiredStruct *TrivialStruct `thrift:"required_struct,6,required" json:"required_struct" db:"required_struct"`
}
// Compile time interface enforcer
var _ thrift.Struct = (*StructWithNoCustomDefaultValues)(nil)

func NewStructWithNoCustomDefaultValues() *StructWithNoCustomDefaultValues {
    return (&StructWithNoCustomDefaultValues{}).setDefaults()
}

func (x *StructWithNoCustomDefaultValues) GetUnqualifiedInteger() int32 {
    return x.UnqualifiedInteger
}

func (x *StructWithNoCustomDefaultValues) GetOptionalInteger() int32 {
    if !x.IsSetOptionalInteger() {
        return 0
    }
    return *x.OptionalInteger
}

func (x *StructWithNoCustomDefaultValues) GetRequiredInteger() int32 {
    return x.RequiredInteger
}

func (x *StructWithNoCustomDefaultValues) GetUnqualifiedStruct() *TrivialStruct {
    if !x.IsSetUnqualifiedStruct() {
        return nil
    }
    return x.UnqualifiedStruct
}

func (x *StructWithNoCustomDefaultValues) GetOptionalStruct() *TrivialStruct {
    if !x.IsSetOptionalStruct() {
        return nil
    }
    return x.OptionalStruct
}

func (x *StructWithNoCustomDefaultValues) GetRequiredStruct() *TrivialStruct {
    if !x.IsSetRequiredStruct() {
        return nil
    }
    return x.RequiredStruct
}

func (x *StructWithNoCustomDefaultValues) SetUnqualifiedIntegerNonCompat(value int32) *StructWithNoCustomDefaultValues {
    x.UnqualifiedInteger = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetUnqualifiedInteger(value int32) *StructWithNoCustomDefaultValues {
    x.UnqualifiedInteger = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetOptionalIntegerNonCompat(value int32) *StructWithNoCustomDefaultValues {
    x.OptionalInteger = &value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetOptionalInteger(value *int32) *StructWithNoCustomDefaultValues {
    x.OptionalInteger = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetRequiredIntegerNonCompat(value int32) *StructWithNoCustomDefaultValues {
    x.RequiredInteger = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetRequiredInteger(value int32) *StructWithNoCustomDefaultValues {
    x.RequiredInteger = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetUnqualifiedStructNonCompat(value *TrivialStruct) *StructWithNoCustomDefaultValues {
    x.UnqualifiedStruct = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetUnqualifiedStruct(value *TrivialStruct) *StructWithNoCustomDefaultValues {
    x.UnqualifiedStruct = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetOptionalStructNonCompat(value *TrivialStruct) *StructWithNoCustomDefaultValues {
    x.OptionalStruct = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetOptionalStruct(value *TrivialStruct) *StructWithNoCustomDefaultValues {
    x.OptionalStruct = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetRequiredStructNonCompat(value *TrivialStruct) *StructWithNoCustomDefaultValues {
    x.RequiredStruct = value
    return x
}

func (x *StructWithNoCustomDefaultValues) SetRequiredStruct(value *TrivialStruct) *StructWithNoCustomDefaultValues {
    x.RequiredStruct = value
    return x
}

func (x *StructWithNoCustomDefaultValues) IsSetOptionalInteger() bool {
    return x != nil && x.OptionalInteger != nil
}

func (x *StructWithNoCustomDefaultValues) IsSetUnqualifiedStruct() bool {
    return x != nil && x.UnqualifiedStruct != nil
}

func (x *StructWithNoCustomDefaultValues) IsSetOptionalStruct() bool {
    return x != nil && x.OptionalStruct != nil
}

func (x *StructWithNoCustomDefaultValues) IsSetRequiredStruct() bool {
    return x != nil && x.RequiredStruct != nil
}

func (x *StructWithNoCustomDefaultValues) writeField1(p thrift.Encoder) error {  // UnqualifiedInteger
    if err := p.WriteFieldBegin("unqualified_integer", thrift.I32, 1); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.UnqualifiedInteger
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithNoCustomDefaultValues) writeField2(p thrift.Encoder) error {  // OptionalInteger
    if !x.IsSetOptionalInteger() {
        return nil
    }

    if err := p.WriteFieldBegin("optional_integer", thrift.I32, 2); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := *x.OptionalInteger
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithNoCustomDefaultValues) writeField3(p thrift.Encoder) error {  // RequiredInteger
    if err := p.WriteFieldBegin("required_integer", thrift.I32, 3); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.RequiredInteger
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithNoCustomDefaultValues) writeField4(p thrift.Encoder) error {  // UnqualifiedStruct
    if !x.IsSetUnqualifiedStruct() {
        return nil
    }

    if err := p.WriteFieldBegin("unqualified_struct", thrift.STRUCT, 4); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.UnqualifiedStruct
    if err := item.Write(p); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithNoCustomDefaultValues) writeField5(p thrift.Encoder) error {  // OptionalStruct
    if !x.IsSetOptionalStruct() {
        return nil
    }

    if err := p.WriteFieldBegin("optional_struct", thrift.STRUCT, 5); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.OptionalStruct
    if err := item.Write(p); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithNoCustomDefaultValues) writeField6(p thrift.Encoder) error {  // RequiredStruct
    if !x.IsSetRequiredStruct() {
        return nil
    }

    if err := p.WriteFieldBegin("required_struct", thrift.STRUCT, 6); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.RequiredStruct
    if err := item.Write(p); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithNoCustomDefaultValues) readField1(p thrift.Decoder) error {  // UnqualifiedInteger
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.UnqualifiedInteger = result
    return nil
}

func (x *StructWithNoCustomDefaultValues) readField2(p thrift.Decoder) error {  // OptionalInteger
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.OptionalInteger = &result
    return nil
}

func (x *StructWithNoCustomDefaultValues) readField3(p thrift.Decoder) error {  // RequiredInteger
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.RequiredInteger = result
    return nil
}

func (x *StructWithNoCustomDefaultValues) readField4(p thrift.Decoder) error {  // UnqualifiedStruct
    result := NewTrivialStruct()
    err := result.Read(p)
    if err != nil {
        return err
    }

    x.UnqualifiedStruct = result
    return nil
}

func (x *StructWithNoCustomDefaultValues) readField5(p thrift.Decoder) error {  // OptionalStruct
    result := NewTrivialStruct()
    err := result.Read(p)
    if err != nil {
        return err
    }

    x.OptionalStruct = result
    return nil
}

func (x *StructWithNoCustomDefaultValues) readField6(p thrift.Decoder) error {  // RequiredStruct
    result := NewTrivialStruct()
    err := result.Read(p)
    if err != nil {
        return err
    }

    x.RequiredStruct = result
    return nil
}


// Deprecated: Use NewStructWithNoCustomDefaultValues().GetUnqualifiedStruct() instead.
func (x *StructWithNoCustomDefaultValues) DefaultGetUnqualifiedStruct() *TrivialStruct {
    if !x.IsSetUnqualifiedStruct() {
        return NewTrivialStruct()
    }
    return x.UnqualifiedStruct
}

// Deprecated: Use NewStructWithNoCustomDefaultValues().GetOptionalStruct() instead.
func (x *StructWithNoCustomDefaultValues) DefaultGetOptionalStruct() *TrivialStruct {
    if !x.IsSetOptionalStruct() {
        return NewTrivialStruct()
    }
    return x.OptionalStruct
}

// Deprecated: Use NewStructWithNoCustomDefaultValues().GetRequiredStruct() instead.
func (x *StructWithNoCustomDefaultValues) DefaultGetRequiredStruct() *TrivialStruct {
    if !x.IsSetRequiredStruct() {
        return NewTrivialStruct()
    }
    return x.RequiredStruct
}



func (x *StructWithNoCustomDefaultValues) Write(p thrift.Encoder) error {
    if err := p.WriteStructBegin("StructWithNoCustomDefaultValues"); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", x), err)
    }

    if err := x.writeField1(p); err != nil {
        return err
    }
    if err := x.writeField2(p); err != nil {
        return err
    }
    if err := x.writeField3(p); err != nil {
        return err
    }
    if err := x.writeField4(p); err != nil {
        return err
    }
    if err := x.writeField5(p); err != nil {
        return err
    }
    if err := x.writeField6(p); err != nil {
        return err
    }

    if err := p.WriteFieldStop(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", x), err)
    }

    if err := p.WriteStructEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", x), err)
    }
    return nil
}

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

    for {
        _, wireType, id, err := p.ReadFieldBegin()
        if err != nil {
            return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", x, id), err)
        }

        if wireType == thrift.STOP {
            break;
        }

        var fieldReadErr error
        switch {
        case (id == 1 && wireType == thrift.Type(thrift.I32)):  // unqualified_integer
            fieldReadErr = x.readField1(p)
        case (id == 2 && wireType == thrift.Type(thrift.I32)):  // optional_integer
            fieldReadErr = x.readField2(p)
        case (id == 3 && wireType == thrift.Type(thrift.I32)):  // required_integer
            fieldReadErr = x.readField3(p)
        case (id == 4 && wireType == thrift.Type(thrift.STRUCT)):  // unqualified_struct
            fieldReadErr = x.readField4(p)
        case (id == 5 && wireType == thrift.Type(thrift.STRUCT)):  // optional_struct
            fieldReadErr = x.readField5(p)
        case (id == 6 && wireType == thrift.Type(thrift.STRUCT)):  // required_struct
            fieldReadErr = x.readField6(p)
        default:
            fieldReadErr = p.Skip(wireType)
        }

        if fieldReadErr != nil {
            return fieldReadErr
        }

        if err := p.ReadFieldEnd(); err != nil {
            return err
        }
    }

    if err := p.ReadStructEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", x), err)
    }

    return nil
}

func (x *StructWithNoCustomDefaultValues) String() string {
    return thrift.StructToString(reflect.ValueOf(x))
}

func (x *StructWithNoCustomDefaultValues) setDefaults() *StructWithNoCustomDefaultValues {
    return x.
        SetUnqualifiedIntegerNonCompat(0).
        SetRequiredIntegerNonCompat(0).
        SetUnqualifiedStructNonCompat(NewTrivialStruct()).
        SetRequiredStructNonCompat(NewTrivialStruct())
}

type StructWithCustomDefaultValues struct {
    UnqualifiedInteger int32 `thrift:"unqualified_integer,1" json:"unqualified_integer" db:"unqualified_integer"`
    OptionalInteger *int32 `thrift:"optional_integer,2,optional" json:"optional_integer,omitempty" db:"optional_integer"`
    RequiredInteger int32 `thrift:"required_integer,3,required" json:"required_integer" db:"required_integer"`
    UnqualifiedStruct *TrivialStruct `thrift:"unqualified_struct,4" json:"unqualified_struct" db:"unqualified_struct"`
    OptionalStruct *TrivialStruct `thrift:"optional_struct,5,optional" json:"optional_struct,omitempty" db:"optional_struct"`
    RequiredStruct *TrivialStruct `thrift:"required_struct,6,required" json:"required_struct" db:"required_struct"`
}
// Compile time interface enforcer
var _ thrift.Struct = (*StructWithCustomDefaultValues)(nil)

func NewStructWithCustomDefaultValues() *StructWithCustomDefaultValues {
    return (&StructWithCustomDefaultValues{}).setDefaults()
}

func (x *StructWithCustomDefaultValues) GetUnqualifiedInteger() int32 {
    return x.UnqualifiedInteger
}

func (x *StructWithCustomDefaultValues) GetOptionalInteger() int32 {
    if !x.IsSetOptionalInteger() {
        return 43
    }
    return *x.OptionalInteger
}

func (x *StructWithCustomDefaultValues) GetRequiredInteger() int32 {
    return x.RequiredInteger
}

func (x *StructWithCustomDefaultValues) GetUnqualifiedStruct() *TrivialStruct {
    if !x.IsSetUnqualifiedStruct() {
        return nil
    }
    return x.UnqualifiedStruct
}

func (x *StructWithCustomDefaultValues) GetOptionalStruct() *TrivialStruct {
    if !x.IsSetOptionalStruct() {
        return nil
    }
    return x.OptionalStruct
}

func (x *StructWithCustomDefaultValues) GetRequiredStruct() *TrivialStruct {
    if !x.IsSetRequiredStruct() {
        return nil
    }
    return x.RequiredStruct
}

func (x *StructWithCustomDefaultValues) SetUnqualifiedIntegerNonCompat(value int32) *StructWithCustomDefaultValues {
    x.UnqualifiedInteger = value
    return x
}

func (x *StructWithCustomDefaultValues) SetUnqualifiedInteger(value int32) *StructWithCustomDefaultValues {
    x.UnqualifiedInteger = value
    return x
}

func (x *StructWithCustomDefaultValues) SetOptionalIntegerNonCompat(value int32) *StructWithCustomDefaultValues {
    x.OptionalInteger = &value
    return x
}

func (x *StructWithCustomDefaultValues) SetOptionalInteger(value int32) *StructWithCustomDefaultValues {
    x.OptionalInteger = &value
    return x
}

func (x *StructWithCustomDefaultValues) SetRequiredIntegerNonCompat(value int32) *StructWithCustomDefaultValues {
    x.RequiredInteger = value
    return x
}

func (x *StructWithCustomDefaultValues) SetRequiredInteger(value int32) *StructWithCustomDefaultValues {
    x.RequiredInteger = value
    return x
}

func (x *StructWithCustomDefaultValues) SetUnqualifiedStructNonCompat(value *TrivialStruct) *StructWithCustomDefaultValues {
    x.UnqualifiedStruct = value
    return x
}

func (x *StructWithCustomDefaultValues) SetUnqualifiedStruct(value *TrivialStruct) *StructWithCustomDefaultValues {
    x.UnqualifiedStruct = value
    return x
}

func (x *StructWithCustomDefaultValues) SetOptionalStructNonCompat(value *TrivialStruct) *StructWithCustomDefaultValues {
    x.OptionalStruct = value
    return x
}

func (x *StructWithCustomDefaultValues) SetOptionalStruct(value TrivialStruct) *StructWithCustomDefaultValues {
    x.OptionalStruct = &value
    return x
}

func (x *StructWithCustomDefaultValues) SetRequiredStructNonCompat(value *TrivialStruct) *StructWithCustomDefaultValues {
    x.RequiredStruct = value
    return x
}

func (x *StructWithCustomDefaultValues) SetRequiredStruct(value *TrivialStruct) *StructWithCustomDefaultValues {
    x.RequiredStruct = value
    return x
}

func (x *StructWithCustomDefaultValues) IsSetOptionalInteger() bool {
    return x != nil && x.OptionalInteger != nil
}

func (x *StructWithCustomDefaultValues) IsSetUnqualifiedStruct() bool {
    return x != nil && x.UnqualifiedStruct != nil
}

func (x *StructWithCustomDefaultValues) IsSetOptionalStruct() bool {
    return x != nil && x.OptionalStruct != nil
}

func (x *StructWithCustomDefaultValues) IsSetRequiredStruct() bool {
    return x != nil && x.RequiredStruct != nil
}

func (x *StructWithCustomDefaultValues) writeField1(p thrift.Encoder) error {  // UnqualifiedInteger
    if err := p.WriteFieldBegin("unqualified_integer", thrift.I32, 1); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.UnqualifiedInteger
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithCustomDefaultValues) writeField2(p thrift.Encoder) error {  // OptionalInteger
    if !x.IsSetOptionalInteger() {
        return nil
    }

    if err := p.WriteFieldBegin("optional_integer", thrift.I32, 2); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := *x.OptionalInteger
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithCustomDefaultValues) writeField3(p thrift.Encoder) error {  // RequiredInteger
    if err := p.WriteFieldBegin("required_integer", thrift.I32, 3); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.RequiredInteger
    if err := p.WriteI32(item); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithCustomDefaultValues) writeField4(p thrift.Encoder) error {  // UnqualifiedStruct
    if !x.IsSetUnqualifiedStruct() {
        return nil
    }

    if err := p.WriteFieldBegin("unqualified_struct", thrift.STRUCT, 4); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.UnqualifiedStruct
    if err := item.Write(p); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithCustomDefaultValues) writeField5(p thrift.Encoder) error {  // OptionalStruct
    if !x.IsSetOptionalStruct() {
        return nil
    }

    if err := p.WriteFieldBegin("optional_struct", thrift.STRUCT, 5); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.OptionalStruct
    if err := item.Write(p); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithCustomDefaultValues) writeField6(p thrift.Encoder) error {  // RequiredStruct
    if !x.IsSetRequiredStruct() {
        return nil
    }

    if err := p.WriteFieldBegin("required_struct", thrift.STRUCT, 6); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field begin error: ", x), err)
    }

    item := x.RequiredStruct
    if err := item.Write(p); err != nil {
        return err
    }

    if err := p.WriteFieldEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field end error: ", x), err)
    }
    return nil
}

func (x *StructWithCustomDefaultValues) readField1(p thrift.Decoder) error {  // UnqualifiedInteger
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.UnqualifiedInteger = result
    return nil
}

func (x *StructWithCustomDefaultValues) readField2(p thrift.Decoder) error {  // OptionalInteger
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.OptionalInteger = &result
    return nil
}

func (x *StructWithCustomDefaultValues) readField3(p thrift.Decoder) error {  // RequiredInteger
    result, err := p.ReadI32()
    if err != nil {
        return err
    }

    x.RequiredInteger = result
    return nil
}

func (x *StructWithCustomDefaultValues) readField4(p thrift.Decoder) error {  // UnqualifiedStruct
    result := NewTrivialStruct()
    err := result.Read(p)
    if err != nil {
        return err
    }

    x.UnqualifiedStruct = result
    return nil
}

func (x *StructWithCustomDefaultValues) readField5(p thrift.Decoder) error {  // OptionalStruct
    result := NewTrivialStruct()
    err := result.Read(p)
    if err != nil {
        return err
    }

    x.OptionalStruct = result
    return nil
}

func (x *StructWithCustomDefaultValues) readField6(p thrift.Decoder) error {  // RequiredStruct
    result := NewTrivialStruct()
    err := result.Read(p)
    if err != nil {
        return err
    }

    x.RequiredStruct = result
    return nil
}


// Deprecated: Use NewStructWithCustomDefaultValues().GetUnqualifiedStruct() instead.
func (x *StructWithCustomDefaultValues) DefaultGetUnqualifiedStruct() *TrivialStruct {
    if !x.IsSetUnqualifiedStruct() {
        return NewTrivialStruct()
    }
    return x.UnqualifiedStruct
}

// Deprecated: Use NewStructWithCustomDefaultValues().GetOptionalStruct() instead.
func (x *StructWithCustomDefaultValues) DefaultGetOptionalStruct() *TrivialStruct {
    if !x.IsSetOptionalStruct() {
        return NewTrivialStruct()
    }
    return x.OptionalStruct
}

// Deprecated: Use NewStructWithCustomDefaultValues().GetRequiredStruct() instead.
func (x *StructWithCustomDefaultValues) DefaultGetRequiredStruct() *TrivialStruct {
    if !x.IsSetRequiredStruct() {
        return NewTrivialStruct()
    }
    return x.RequiredStruct
}



func (x *StructWithCustomDefaultValues) Write(p thrift.Encoder) error {
    if err := p.WriteStructBegin("StructWithCustomDefaultValues"); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write struct begin error: ", x), err)
    }

    if err := x.writeField1(p); err != nil {
        return err
    }
    if err := x.writeField2(p); err != nil {
        return err
    }
    if err := x.writeField3(p); err != nil {
        return err
    }
    if err := x.writeField4(p); err != nil {
        return err
    }
    if err := x.writeField5(p); err != nil {
        return err
    }
    if err := x.writeField6(p); err != nil {
        return err
    }

    if err := p.WriteFieldStop(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write field stop error: ", x), err)
    }

    if err := p.WriteStructEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T write struct end error: ", x), err)
    }
    return nil
}

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

    for {
        _, wireType, id, err := p.ReadFieldBegin()
        if err != nil {
            return thrift.PrependError(fmt.Sprintf("%T field %d read error: ", x, id), err)
        }

        if wireType == thrift.STOP {
            break;
        }

        var fieldReadErr error
        switch {
        case (id == 1 && wireType == thrift.Type(thrift.I32)):  // unqualified_integer
            fieldReadErr = x.readField1(p)
        case (id == 2 && wireType == thrift.Type(thrift.I32)):  // optional_integer
            fieldReadErr = x.readField2(p)
        case (id == 3 && wireType == thrift.Type(thrift.I32)):  // required_integer
            fieldReadErr = x.readField3(p)
        case (id == 4 && wireType == thrift.Type(thrift.STRUCT)):  // unqualified_struct
            fieldReadErr = x.readField4(p)
        case (id == 5 && wireType == thrift.Type(thrift.STRUCT)):  // optional_struct
            fieldReadErr = x.readField5(p)
        case (id == 6 && wireType == thrift.Type(thrift.STRUCT)):  // required_struct
            fieldReadErr = x.readField6(p)
        default:
            fieldReadErr = p.Skip(wireType)
        }

        if fieldReadErr != nil {
            return fieldReadErr
        }

        if err := p.ReadFieldEnd(); err != nil {
            return err
        }
    }

    if err := p.ReadStructEnd(); err != nil {
        return thrift.PrependError(fmt.Sprintf("%T read struct end error: ", x), err)
    }

    return nil
}

func (x *StructWithCustomDefaultValues) String() string {
    return thrift.StructToString(reflect.ValueOf(x))
}

func (x *StructWithCustomDefaultValues) setDefaults() *StructWithCustomDefaultValues {
    return x.
        SetUnqualifiedIntegerNonCompat(42).
        SetOptionalIntegerNonCompat(43).
        SetRequiredIntegerNonCompat(44).
        SetUnqualifiedStructNonCompat(
              NewTrivialStruct().
    SetIntValueNonCompat(123),
          ).
        SetOptionalStructNonCompat(
              NewTrivialStruct().
    SetIntValueNonCompat(456),
          ).
        SetRequiredStructNonCompat(
              NewTrivialStruct().
    SetIntValueNonCompat(789),
          )
}



// RegisterTypes registers types found in this file that have a thrift_uri with the passed in registry.
func RegisterTypes(registry interface {
  RegisterType(name string, initializer func() any)
}) {
    registry.RegisterType("facebook.com/thrift/compiler/test/fixtures/default_values/TrivialStruct", func() any { return NewTrivialStruct() })
    registry.RegisterType("facebook.com/thrift/compiler/test/fixtures/default_values/StructWithNoCustomDefaultValues", func() any { return NewStructWithNoCustomDefaultValues() })
    registry.RegisterType("facebook.com/thrift/compiler/test/fixtures/default_values/StructWithCustomDefaultValues", func() any { return NewStructWithCustomDefaultValues() })

}
