github.com/cs3org/reva/v2@v2.27.7/pkg/sdk/common/opaque.go (about)

     1  // Copyright 2018-2021 CERN
     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  // In applying this license, CERN does not waive the privileges and immunities
    16  // granted to it by virtue of its status as an Intergovernmental Organization
    17  // or submit itself to any jurisdiction.
    18  
    19  package common
    20  
    21  import (
    22  	"encoding/json"
    23  	"encoding/xml"
    24  	"fmt"
    25  
    26  	"github.com/BurntSushi/toml"
    27  	types "github.com/cs3org/go-cs3apis/cs3/types/v1beta1"
    28  )
    29  
    30  // DecodeOpaqueMap decodes a Reva opaque object into a map of strings.
    31  func DecodeOpaqueMap(opaque *types.Opaque) map[string]string {
    32  	entries := make(map[string]string)
    33  
    34  	if opaque != nil {
    35  		for k, v := range opaque.GetMap() {
    36  			switch v.Decoder {
    37  			case "plain":
    38  				entries[k] = string(v.Value)
    39  			case "json":
    40  				var s string
    41  				_ = json.Unmarshal(v.Value, &s)
    42  				entries[k] = s
    43  			case "toml":
    44  				var s string
    45  				_ = toml.Unmarshal(v.Value, &s)
    46  				entries[k] = s
    47  			case "xml":
    48  				var s string
    49  				_ = xml.Unmarshal(v.Value, &s)
    50  				entries[k] = s
    51  			}
    52  		}
    53  	}
    54  
    55  	return entries
    56  }
    57  
    58  // EncodeOpaqueMap encodes a map of strings into a Reva opaque entry.
    59  // Only plain encoding is currently supported.
    60  func EncodeOpaqueMap(opaque *types.Opaque, m map[string]string) {
    61  	if opaque == nil {
    62  		return
    63  	}
    64  	if opaque.Map == nil {
    65  		opaque.Map = map[string]*types.OpaqueEntry{}
    66  	}
    67  
    68  	for k, v := range m {
    69  		// Only plain values are currently supported
    70  		opaque.Map[k] = &types.OpaqueEntry{
    71  			Decoder: "plain",
    72  			Value:   []byte(v),
    73  		}
    74  	}
    75  
    76  }
    77  
    78  // GetValuesFromOpaque extracts the given keys from the opaque object.
    79  // If mandatory is set to true, all specified keys must be available in the opaque object.
    80  func GetValuesFromOpaque(opaque *types.Opaque, keys []string, mandatory bool) (map[string]string, error) {
    81  	values := make(map[string]string)
    82  	entries := DecodeOpaqueMap(opaque)
    83  
    84  	for _, key := range keys {
    85  		if value, ok := entries[key]; ok {
    86  			values[key] = value
    87  		} else if mandatory {
    88  			return map[string]string{}, fmt.Errorf("missing opaque entry '%v'", key)
    89  		}
    90  	}
    91  
    92  	return values, nil
    93  }