github.com/ystia/yorc/v4@v4.3.0/storage/encoding/gob.go (about)

     1  // Copyright 2019 Bull S.A.S. Atos Technologies - Bull, Rue Jean Jaures, B.P.68, 78340, Les Clayes-sous-Bois, France.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package encoding
    16  
    17  import (
    18  	"bytes"
    19  	"encoding/gob"
    20  )
    21  
    22  // GobCodec encodes/decodes Go values to/from gob.
    23  // You can use encoding.Gob instead of creating an instance of this struct.
    24  type GobCodec struct{}
    25  
    26  // Marshal encodes a Go value to gob.
    27  func (c GobCodec) Marshal(v interface{}) ([]byte, error) {
    28  	buffer := new(bytes.Buffer)
    29  	encoder := gob.NewEncoder(buffer)
    30  	err := encoder.Encode(v)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  	return buffer.Bytes(), nil
    35  }
    36  
    37  // Unmarshal decodes a gob value into a Go value.
    38  func (c GobCodec) Unmarshal(data []byte, v interface{}) error {
    39  	reader := bytes.NewReader(data)
    40  	decoder := gob.NewDecoder(reader)
    41  	return decoder.Decode(v)
    42  }