options.go

 1package ygo
 2
 3/*
 4#include "libyrs.h"
 5*/
 6import "C"
 7
 8// DocOption configures document creation.
 9type DocOption func(*DocOptions)
10
11// OffsetKind determines how text offsets are counted.
12type OffsetKind uint8
13
14const (
15	// OffsetBytes counts text offsets by UTF-8 bytes
16	OffsetBytes OffsetKind = C.Y_OFFSET_BYTES
17	// OffsetUTF16 counts text offsets by UTF-16 code units
18	OffsetUTF16 OffsetKind = C.Y_OFFSET_UTF16
19)
20
21// DocOptions configures document creation.
22type DocOptions struct {
23	// ClientID is the globally unique 53-bit identifier for this document replica.
24	// If 0, a random ID is generated.
25	ClientID uint64
26
27	// GUID is a globally unique UUID v4 compatible string identifier.
28	// If empty, a random UUID is generated.
29	GUID string
30
31	// CollectionID is an optional collection identifier for providers.
32	CollectionID string
33
34	// Encoding determines text offset calculation (bytes or UTF-16).
35	Encoding OffsetKind
36
37	// SkipGC disables garbage collection of deleted blocks when true.
38	SkipGC bool
39
40	// AutoLoad automatically loads subdocuments when true.
41	AutoLoad bool
42
43	// ShouldLoad determines if the document should be synced by providers.
44	ShouldLoad bool
45}
46
47// DefaultOptions returns default document options.
48func DefaultOptions() DocOptions {
49	var cOpts C.YOptions = C.yoptions()
50	flags := uint8(cOpts.flags)
51	return DocOptions{
52		ClientID:   uint64(cOpts.id),
53		Encoding:   OffsetKind(flags & 0x01),
54		SkipGC:     flags&C.Y_SKIP_GC != 0,
55		AutoLoad:   flags&C.Y_AUTO_LOAD != 0,
56		ShouldLoad: flags&C.Y_SHOULD_LOAD != 0,
57	}
58}
59
60// toC converts DocOptions to C.YOptions.
61func (o DocOptions) toC() C.YOptions {
62	var cOpts C.YOptions
63	cOpts.id = C.uint64_t(o.ClientID)
64	var flags C.uint8_t
65	flags = C.uint8_t(o.Encoding)
66	if o.SkipGC {
67		flags |= C.Y_SKIP_GC
68	}
69	if o.AutoLoad {
70		flags |= C.Y_AUTO_LOAD
71	}
72	if o.ShouldLoad {
73		flags |= C.Y_SHOULD_LOAD
74	}
75	cOpts.flags = flags
76	if o.GUID != "" {
77		cOpts.guid = stringToC(o.GUID)
78	}
79	if o.CollectionID != "" {
80		cOpts.collection_id = stringToC(o.CollectionID)
81	}
82	return cOpts
83}