gitee.com/go-spring2/spring-base@v1.1.3/cast/bean.go (about)

     1  /*
     2   * Copyright 2012-2019 the original author or authors.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *      https://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cast
    18  
    19  import (
    20  	"encoding/json"
    21  )
    22  
    23  var (
    24  	FAST = &fastEncoding{}
    25  	JSON = &jsonEncoding{}
    26  )
    27  
    28  type fastEncoding struct{}
    29  
    30  type ConversionArg struct {
    31  	DeepCopy bool
    32  }
    33  
    34  // Convert converts src to dest using fast encoding.
    35  func (e *fastEncoding) Convert(src interface{}, dest interface{}, arg ...ConversionArg) error {
    36  	return nil
    37  }
    38  
    39  type jsonEncoding struct{}
    40  
    41  // Convert converts src to dest using json encoding.
    42  func (e *jsonEncoding) Convert(src interface{}, dest interface{}) error {
    43  	b, err := json.Marshal(src)
    44  	if err != nil {
    45  		return err
    46  	}
    47  	return json.Unmarshal(b, dest)
    48  }