github.com/webdestroya/awsmocker@v0.2.6/util.go (about)

     1  package awsmocker
     2  
     3  import (
     4  	"bufio"
     5  	"encoding/json"
     6  	"encoding/xml"
     7  	"errors"
     8  	"io"
     9  	"strings"
    10  )
    11  
    12  // var awsDomainRegexp = regexp.MustCompile(`(amazonaws\.com|\.aws)$`)
    13  
    14  func encodeAsXml(obj any) string {
    15  	out, err := xml.MarshalIndent(obj, "", "  ")
    16  	if err != nil {
    17  		panic(err)
    18  	}
    19  
    20  	return string(out)
    21  }
    22  
    23  func EncodeAsJson(obj any) string {
    24  	out, err := json.Marshal(obj)
    25  	if err != nil {
    26  		panic(err)
    27  	}
    28  
    29  	return string(out)
    30  }
    31  
    32  func inferContentType(value string) string {
    33  	switch {
    34  	case strings.HasPrefix(value, "<"):
    35  		return ContentTypeXML
    36  	case strings.HasPrefix(value, "{"):
    37  		return ContentTypeJSON
    38  	default:
    39  		return ContentTypeText
    40  	}
    41  }
    42  
    43  func isEof(r *bufio.Reader) bool {
    44  	_, err := r.Peek(1)
    45  	return errors.Is(err, io.EOF)
    46  }
    47  
    48  /*
    49  // whether this is an AWS hostname that should be handled
    50  func isAwsHostname(hostname string) bool {
    51  	if strings.HasSuffix(hostname, "amazonaws.com") {
    52  		return true
    53  	}
    54  
    55  	if strings.HasSuffix(hostname, "aws") {
    56  		return true
    57  	}
    58  
    59  	if strings.HasSuffix(hostname, "amazonaws.com.cn") {
    60  		return true
    61  	}
    62  
    63  	return false
    64  }
    65  */