func GetEncodedItemKey()

in dax/internal/cbor/item.go [44:168]


func GetEncodedItemKey(item map[string]types.AttributeValue, keydef []types.AttributeDefinition) ([]byte, error) {
	if item == nil {
		return nil, &smithy.GenericAPIError{
			Code:    ErrCodeValidationException,
			Message: "item cannot be nil",
		}
	}

	hk := keydef[0]
	hkval, foundKey := item[*hk.AttributeName]
	if !foundKey {
		return nil, ErrMissingKey
	}

	var buf bytes.Buffer
	w := NewWriter(&buf)
	defer w.Close()

	if len(keydef) == 1 {
		switch hk.AttributeType {
		case types.ScalarAttributeTypeS:
			sp, isExpectedType := hkval.(*types.AttributeValueMemberS)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := w.Write([]byte(sp.Value)); err != nil {
				return nil, err
			}
		case types.ScalarAttributeTypeN:
			_, isExpectedType := hkval.(*types.AttributeValueMemberN)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := EncodeAttributeValue(hkval, w); err != nil {
				return nil, err
			}
		case types.ScalarAttributeTypeB:
			b, isExpectedType := hkval.(*types.AttributeValueMemberB)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := w.Write(b.Value); err != nil {
				return nil, err
			}
		default:
			return nil, fmt.Errorf("unsupported KeyType encountered in Hash Attribute: %s", hk.AttributeType)
		}
	} else {
		switch hk.AttributeType {
		case types.ScalarAttributeTypeS:
			sp, isExpectedType := hkval.(*types.AttributeValueMemberS)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := w.WriteString(sp.Value); err != nil {
				return nil, err
			}
		case types.ScalarAttributeTypeN:
			_, isExpectedType := hkval.(*types.AttributeValueMemberN)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := EncodeAttributeValue(hkval, w); err != nil {
				return nil, err
			}
		case types.ScalarAttributeTypeB:
			b, isExpectedType := hkval.(*types.AttributeValueMemberB)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := w.WriteBytes(b.Value); err != nil {
				return nil, err
			}
		default:
			return nil, fmt.Errorf("unsupported KeyType encountered in Hash Attribute: %s", hk.AttributeType)
		}

		rk := keydef[1]
		rkval, foundKey := item[*rk.AttributeName]
		if !foundKey {
			return nil, ErrMissingKey
		}
		switch rk.AttributeType {
		case types.ScalarAttributeTypeS:
			sp, isExpectedType := rkval.(*types.AttributeValueMemberS)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := w.Write([]byte(sp.Value)); err != nil {
				return nil, err
			}
		case types.ScalarAttributeTypeN:
			n, isExpectedType := rkval.(*types.AttributeValueMemberN)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			d := new(Decimal)
			d, isExpectedType = d.SetString(n.Value)
			if !isExpectedType {
				return nil, &smithy.GenericAPIError{
					Code:    ErrCodeValidationException,
					Message: "invalid number " + n.Value,
				}
			}
			if _, err := EncodeLexDecimal(d, w.bw); err != nil {
				return nil, err
			}
		case types.ScalarAttributeTypeB:
			b, isExpectedType := rkval.(*types.AttributeValueMemberB)
			if !isExpectedType {
				return nil, ErrMissingKey
			}
			if err := w.Write(b.Value); err != nil {
				return nil, err
			}
		default:
			return nil, fmt.Errorf("unsupported KeyType encountered in Range Attribute: %s", rk.AttributeType)
		}
	}

	if err := w.Flush(); err != nil {
		return nil, err
	}
	return buf.Bytes(), nil
}