Skip to content

Commit a4192e4

Browse files
author
Ivan Dlugos
committed
Go - add id-companion and date-nano annotations
1 parent 414fa35 commit a4192e4

5 files changed

Lines changed: 811 additions & 68 deletions

File tree

internal/generator/go/ast-reader.go

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,20 @@ var supportedEntityAnnotations = map[string]bool{
4545
}
4646

4747
var supportedPropertyAnnotations = map[string]bool{
48-
"-": true,
49-
"converter": true,
50-
"date": true,
51-
"id": true,
52-
"index": true,
53-
"inline": true,
54-
"lazy": true,
55-
"link": true,
56-
"name": true,
57-
"type": true,
58-
"uid": true,
59-
"unique": true,
48+
"-": true,
49+
"converter": true,
50+
"date": true,
51+
"date-nano": true,
52+
"id": true,
53+
"id-companion": true,
54+
"index": true,
55+
"inline": true,
56+
"lazy": true,
57+
"link": true,
58+
"name": true,
59+
"type": true,
60+
"uid": true,
61+
"unique": true,
6062
}
6163

6264
// astReader contains information about the processed set of Entities
@@ -343,7 +345,7 @@ func (entity *Entity) addFields(parent *Field, fields fieldList, fieldPath, pref
343345

344346
} else if field.Type == "time.Time" {
345347
// first, try to handle time.Time struct - automatically set a converter if it's declared a date by the user
346-
if property.annotations["date"] == nil {
348+
if property.annotations["date"] == nil && property.annotations["date-nano"] == nil {
347349
property.annotations["date"] = &binding.Annotation{}
348350
propertyLog("Notice: time.Time is stored and read using millisecond precision in UTC by default on", property)
349351
log.Printf("To silence this notice either define your own converter using `converter` and " +
@@ -359,6 +361,9 @@ func (entity *Entity) addFields(parent *Field, fields fieldList, fieldPath, pref
359361

360362
if property.annotations["converter"] == nil {
361363
var converter = "objectbox.TimeInt64Convert"
364+
if property.annotations["date-nano"] != nil {
365+
converter = "objectbox.NanoTimeInt64Convert"
366+
}
362367
property.Converter = &converter
363368
property.annotations["type"] = &binding.Annotation{Value: "int64"}
364369
}

test/comparison/testdata/go/typeful/objectbox-model.go.expected

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ func ObjectBoxModel() *objectbox.Model {
1515
model.RegisterBinding(AliasesBinding)
1616
model.RegisterBinding(NillableBinding)
1717
model.RegisterBinding(TypefulBinding)
18-
model.LastEntityId(3, 959367522974354090)
19-
model.LastIndexId(2, 8902041070398994519)
18+
model.RegisterBinding(TSDateBinding)
19+
model.RegisterBinding(TSDateNanoBinding)
20+
model.LastEntityId(5, 1395437218309923052)
21+
model.LastIndexId(3, 406703151708498928)
2022

2123
return model
2224
}

test/comparison/testdata/go/typeful/objectbox-model.json.expected

Lines changed: 72 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -167,135 +167,180 @@
167167
},
168168
{
169169
"id": "3:959367522974354090",
170-
"lastPropertyId": "22:303089054982227392",
170+
"lastPropertyId": "23:7847956203786849690",
171171
"name": "Typeful",
172172
"properties": [
173173
{
174-
"id": "1:2914295034816259174",
174+
"id": "1:6745438398739480977",
175175
"name": "Id",
176176
"type": 6,
177177
"flags": 129
178178
},
179179
{
180-
"id": "2:1395437218309923052",
180+
"id": "2:2897681629866238117",
181181
"name": "Int",
182182
"type": 6
183183
},
184184
{
185-
"id": "3:6745438398739480977",
185+
"id": "3:3398579248012586914",
186186
"name": "Int8",
187187
"type": 2
188188
},
189189
{
190-
"id": "4:2897681629866238117",
190+
"id": "4:5974317550424871033",
191191
"name": "Int16",
192192
"type": 3
193193
},
194194
{
195-
"id": "5:3398579248012586914",
195+
"id": "5:3317123977833389635",
196196
"name": "Int32",
197197
"type": 5
198198
},
199199
{
200-
"id": "6:5974317550424871033",
200+
"id": "6:5001958211167890979",
201201
"name": "Int64",
202202
"type": 6
203203
},
204204
{
205-
"id": "7:3317123977833389635",
205+
"id": "7:167566062957544642",
206206
"name": "Uint",
207207
"type": 6,
208208
"flags": 8192
209209
},
210210
{
211-
"id": "8:5001958211167890979",
211+
"id": "8:4778690082005258714",
212212
"name": "Uint8",
213213
"type": 2,
214214
"flags": 8192
215215
},
216216
{
217-
"id": "9:167566062957544642",
217+
"id": "9:1059542851699319360",
218218
"name": "Uint16",
219219
"type": 3,
220220
"flags": 8192
221221
},
222222
{
223-
"id": "10:4778690082005258714",
223+
"id": "10:6972732843819909978",
224224
"name": "Uint32",
225225
"type": 5,
226226
"flags": 8192
227227
},
228228
{
229-
"id": "11:1059542851699319360",
229+
"id": "11:5558237345453186302",
230230
"name": "Uint64",
231231
"type": 6,
232232
"flags": 8192
233233
},
234234
{
235-
"id": "12:6972732843819909978",
235+
"id": "12:7845762441295307478",
236236
"name": "Bool",
237237
"type": 1
238238
},
239239
{
240-
"id": "13:5558237345453186302",
240+
"id": "13:771642788862502430",
241241
"name": "String",
242242
"type": 9
243243
},
244244
{
245-
"id": "14:7845762441295307478",
245+
"id": "14:8514850266767180993",
246246
"name": "StringVector",
247247
"type": 30
248248
},
249249
{
250-
"id": "15:771642788862502430",
250+
"id": "15:8683452355129068124",
251251
"name": "Byte",
252252
"type": 2,
253253
"flags": 8192
254254
},
255255
{
256-
"id": "16:8514850266767180993",
256+
"id": "16:4345851588384648695",
257257
"name": "ByteVector",
258258
"type": 23
259259
},
260260
{
261-
"id": "17:8683452355129068124",
261+
"id": "17:7699391924090763411",
262262
"name": "Rune",
263263
"type": 5
264264
},
265265
{
266-
"id": "18:4345851588384648695",
266+
"id": "18:388440063886460141",
267267
"name": "Float32",
268268
"type": 7
269269
},
270270
{
271-
"id": "19:7699391924090763411",
271+
"id": "19:7561811714888168464",
272272
"name": "Float64",
273273
"type": 8
274274
},
275275
{
276-
"id": "20:388440063886460141",
276+
"id": "20:3959279844101328186",
277277
"name": "Date",
278-
"indexId": "1:7561811714888168464",
278+
"indexId": "1:8902041070398994519",
279279
"type": 10,
280280
"flags": 8
281281
},
282282
{
283-
"id": "21:3959279844101328186",
283+
"id": "21:303089054982227392",
284284
"name": "Time",
285-
"indexId": "2:8902041070398994519",
285+
"indexId": "2:7338728586234333996",
286286
"type": 10,
287287
"flags": 8
288288
},
289289
{
290-
"id": "22:303089054982227392",
290+
"id": "22:5392504858645185670",
291291
"name": "Time2",
292292
"type": 10
293+
},
294+
{
295+
"id": "23:7847956203786849690",
296+
"name": "TimeNano",
297+
"indexId": "3:406703151708498928",
298+
"type": 12,
299+
"flags": 8
300+
}
301+
]
302+
},
303+
{
304+
"id": "4:2914295034816259174",
305+
"lastPropertyId": "2:5837486892148644279",
306+
"name": "TSDate",
307+
"properties": [
308+
{
309+
"id": "1:4756106358532488297",
310+
"name": "Id",
311+
"type": 6,
312+
"flags": 1
313+
},
314+
{
315+
"id": "2:5837486892148644279",
316+
"name": "timestamp",
317+
"type": 10,
318+
"flags": 16384
319+
}
320+
]
321+
},
322+
{
323+
"id": "5:1395437218309923052",
324+
"lastPropertyId": "2:2264299874001785192",
325+
"name": "TSDateNano",
326+
"properties": [
327+
{
328+
"id": "1:4736217237333769909",
329+
"name": "Id",
330+
"type": 6,
331+
"flags": 1
332+
},
333+
{
334+
"id": "2:2264299874001785192",
335+
"name": "timestamp",
336+
"type": 12,
337+
"flags": 16384
293338
}
294339
]
295340
}
296341
],
297-
"lastEntityId": "3:959367522974354090",
298-
"lastIndexId": "2:8902041070398994519",
342+
"lastEntityId": "5:1395437218309923052",
343+
"lastIndexId": "3:406703151708498928",
299344
"lastRelationId": "",
300345
"modelVersion": 5,
301346
"modelVersionParserMinimum": 5,

test/comparison/testdata/go/typeful/typeful.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,15 @@ type Typeful struct {
2626
Date int64 `objectbox:"date index"`
2727
Time time.Time `objectbox:"date,index"`
2828
Time2 time.Time // prints a warning, otherwise the same as with an annotation
29+
TimeNano time.Time `objectbox:"date-nano,index"`
30+
}
31+
32+
type TSDate struct {
33+
Id uint64
34+
timestamp int64 `objectbox:"id-companion,date"`
35+
}
36+
37+
type TSDateNano struct {
38+
Id uint64
39+
timestamp int64 `objectbox:"id-companion,date-nano"`
2940
}

0 commit comments

Comments
 (0)