/*
 * Copyright 2024 CloudWeGo Authors
 *
 * Licensed 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 ttstream

import (
	"fmt"

	"github.com/cloudwego/frugal"
	"github.com/cloudwego/gopkg/protocol/thrift"

	kutils "github.com/cloudwego/kitex/pkg/utils"
)

type testRequest struct {
	A int32  `thrift:"A,1" frugal:"1,default,i32" json:"A"`
	B string `thrift:"B,2" frugal:"2,default,string" json:"B"`
}

func (p *testRequest) FastRead(buf []byte) (int, error) {
	return frugal.DecodeObject(buf, p)
}

func (p *testRequest) FastWriteNocopy(buf []byte, binaryWriter thrift.NocopyWriter) int {
	n, _ := frugal.EncodeObject(buf, binaryWriter, p)
	return n
}

func (p *testRequest) BLength() int {
	return frugal.EncodedSize(p)
}

func (p *testRequest) DeepCopy(s interface{}) error {
	src, ok := s.(*testRequest)
	if !ok {
		return fmt.Errorf("%T's type not matched %T", s, p)
	}

	p.A = src.A

	if src.B != "" {
		p.B = kutils.StringDeepCopy(src.B)
	}

	return nil
}

type testResponse = testRequest
