File: hit.go

package info (click to toggle)
golang-github-meilisearch-meilisearch-go 0.33.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 956 kB
  • sloc: makefile: 9
file content (79 lines) | stat: -rw-r--r-- 2,078 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package meilisearch

import (
	"encoding/json"
	"errors"
	"fmt"
	"reflect"
)

type (
	Hit  map[string]json.RawMessage // Hit is a map of key and value raw buffer
	Hits []Hit                      // Hits is an alias for a slice of Hit.
)

// Decode decodes a single Hit into the provided struct.
func (h Hit) Decode(vPtr interface{}) error {
	if vPtr == nil || reflect.ValueOf(vPtr).Kind() != reflect.Ptr {
		return errors.New("vPtr must be a non-nil pointer")
	}

	raw, err := json.Marshal(h)
	if err != nil {
		return fmt.Errorf("failed to marshal hit: %w", err)
	}

	return json.Unmarshal(raw, vPtr)
}

// DecodeWith decodes a Hit into the provided struct using the provided marshal and unmarshal functions.
func (h Hit) DecodeWith(vPtr interface{}, marshal JSONMarshal, unmarshal JSONUnmarshal) error {
	if vPtr == nil || reflect.ValueOf(vPtr).Kind() != reflect.Ptr {
		return errors.New("vPtr must be a non-nil pointer")
	}

	raw, err := marshal(h)
	if err != nil {
		return fmt.Errorf("failed to marshal hit: %w", err)
	}

	return unmarshal(raw, vPtr)
}

func (h Hits) Len() int {
	return len(h)
}

// Decode decodes the Hits into the provided target slice.
func (h Hits) Decode(vSlicePtr interface{}) error {
	v := reflect.ValueOf(vSlicePtr)

	if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Slice {
		return fmt.Errorf("v must be a pointer to a slice, got %T", vSlicePtr)
	}

	raw := []Hit(h)
	buf, err := json.Marshal(raw)
	if err != nil {
		return fmt.Errorf("failed to marshal hits: %w", err)
	}

	return json.Unmarshal(buf, vSlicePtr)
}

// DecodeWith decodes a Hits into the provided struct using the provided marshal and unmarshal functions.
func (h Hits) DecodeWith(vSlicePtr interface{}, marshal JSONMarshal, unmarshal JSONUnmarshal) error {
	v := reflect.ValueOf(vSlicePtr)

	if v.Kind() != reflect.Ptr || v.Elem().Kind() != reflect.Slice {
		return errors.New("v must be a pointer to a slice")
	}

	raw := []Hit(h)
	buf, err := marshal(raw)
	if err != nil {
		return fmt.Errorf("failed to marshal hit: %w", err)
	}

	return unmarshal(buf, vSlicePtr)
}