...

Source file src/solace.dev/go/messaging/pkg/solace/config/format_utils.go

Documentation: solace.dev/go/messaging/pkg/solace/config

     1  // pubsubplus-go-client
     2  //
     3  // Copyright 2021-2024 Solace Corporation. All rights reserved.
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  // http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package config
    18  
    19  import (
    20  	"encoding/json"
    21  	"fmt"
    22  	"strings"
    23  )
    24  
    25  // For internal use only. Do not use.
    26  var errIllegalKey = fmt.Errorf("path cannot be both a node and a leaf")
    27  
    28  // For internal use only. Do not use.
    29  func flattenMap(m map[string]interface{}) map[string]interface{} {
    30  	result := make(map[string]interface{})
    31  	for key, val := range m {
    32  		castedMap, ok := val.(map[string]interface{})
    33  		if ok {
    34  			nestedResult := flattenMap(castedMap)
    35  			for nestedKey, nestedVal := range nestedResult {
    36  				result[key+"."+nestedKey] = nestedVal
    37  			}
    38  		} else {
    39  			result[key] = val
    40  		}
    41  	}
    42  	return result
    43  }
    44  
    45  // For internal use only. Do not use.
    46  func nestMap(m map[string]interface{}) (map[string]interface{}, error) {
    47  	result := make(map[string]interface{})
    48  	for k, v := range m {
    49  		keys := strings.Split(k, ".")
    50  		nested := result
    51  		for i, key := range keys {
    52  			if i+1 == len(keys) {
    53  				_, ok := nested[key]
    54  				if ok {
    55  					return nil, errIllegalKey
    56  				}
    57  				nested[key] = v
    58  			} else {
    59  				val, ok := nested[key]
    60  				if !ok {
    61  					val = make(map[string]interface{})
    62  					nested[key] = val
    63  				}
    64  				castedVal, ok := val.(map[string]interface{})
    65  				if !ok {
    66  					return nil, errIllegalKey
    67  				}
    68  				nested = castedVal
    69  			}
    70  		}
    71  	}
    72  	return result, nil
    73  }
    74  
    75  // For internal use only. Do not use.
    76  func flattenJSON(b []byte) (map[string]interface{}, error) {
    77  	parsed := make(map[string]interface{})
    78  	err := json.Unmarshal(b, &parsed)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  	return flattenMap(parsed), nil
    83  }
    84  
    85  // For internal use only. Do not use.
    86  func nestJSON(m map[string]interface{}) ([]byte, error) {
    87  	nestedMap, err := nestMap(m)
    88  	if err != nil {
    89  		return nil, err
    90  	}
    91  	return json.Marshal(nestedMap)
    92  }
    93