Skip to content

Commit e806fbd

Browse files
committed
Add some more VersionLib*() like VersionLibMinRecommended() and check for minimum and recommended version when creating a new builder
1 parent af6e78f commit e806fbd

File tree

3 files changed

+80
-33
lines changed

3 files changed

+80
-33
lines changed

objectbox/builder.go

+10-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2018-2022 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -45,31 +45,16 @@ type Builder struct {
4545

4646
// NewBuilder creates a new ObjectBox instance builder object
4747
func NewBuilder() *Builder {
48-
// these constants are based on the objectbox.h file, not on the loaded library
49-
var expectedVersion = Version{C.OBX_VERSION_MAJOR, C.OBX_VERSION_MINOR, C.OBX_VERSION_PATCH, ""}
50-
51-
// NOTE, this is currently not used as the C-API currently still breaks compatibility before v1.0.
52-
//if !C.obx_version_is_at_least(C.int(expectedVersion.Major), C.int(expectedVersion.Minor), C.int(expectedVersion.Patch)) {
53-
// var version string
54-
// msg := C.obx_version_string()
55-
// if msg == nil {
56-
// version = "unknown"
57-
// } else {
58-
// version = C.GoString(msg)
59-
// }
60-
// panic("Minimum libobjectbox version " + expectedVersion.String() + " required, but found " + version + "\n" +
61-
// "Please run install.sh for a full upgrade\n" +
62-
// "Or check https://github.com/objectbox/objectbox-c for info about the required library.")
63-
//}
64-
65-
// Therefore, we're checking the exact version match
6648
var version = VersionLib()
67-
// patch version can be higher but only if it's not a "dev" version (100+)
68-
var patchMatches = version.Patch >= expectedVersion.Patch && (version.Patch < 100 || version.Patch == expectedVersion.Patch)
69-
if version.Major != expectedVersion.Major || version.Minor != expectedVersion.Minor || !patchMatches {
70-
panic("ObjectBox native lib version " + expectedVersion.String() + " required, but found " + version.String() + ".\n" +
71-
"Please run install.sh for a full upgrade.\n" +
72-
"Or check https://github.com/objectbox/objectbox-c for info about the required library.")
49+
if version.LessThan(VersionLibMin()) {
50+
panic("The loaded ObjectBox C library is too old for this build of ObjectBox Go.\n" +
51+
"Found version " + version.String() + ", but at least " + VersionLibMin().String() + " is required.\n" +
52+
"Please see https://github.com/objectbox/objectbox-go on how to upgrade.\n" +
53+
"Or, check https://github.com/objectbox/objectbox-c for the C library.")
54+
} else if version.LessThan(VersionLibMinRecommended()) {
55+
println("Note: the loaded ObjectBox C library should be updated.\n" +
56+
" Found ObjectBox version " + version.String() + ", but the minimum recommended version is " +
57+
VersionLibMinRecommended().String() + ".")
7358
}
7459

7560
return &Builder{

objectbox/version.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2021 ObjectBox Ltd. All rights reserved.
2+
* Copyright 2018-2022 ObjectBox Ltd. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,6 +32,14 @@ type Version struct {
3232
Label string
3333
}
3434

35+
func (v Version) LessThan(other Version) bool {
36+
return v.Major < other.Major || v.Minor < other.Minor || v.Patch < other.Patch
37+
}
38+
39+
func (v Version) GreaterThanOrEqualTo(other Version) bool {
40+
return !v.LessThan(other)
41+
}
42+
3543
func (v Version) String() string {
3644
versionString := fmt.Sprintf("%d.%d.%d", v.Major, v.Minor, v.Patch)
3745
if len(v.Label) > 0 {
@@ -46,7 +54,7 @@ func VersionGo() Version {
4654
return Version{1, 6, 0, ""}
4755
}
4856

49-
// VersionLib returns the Version of the dynamic linked ObjectBox library
57+
// VersionLib returns the Version of the dynamic linked ObjectBox library (loaded at runtime)
5058
func VersionLib() Version {
5159
var major C.int
5260
var minor C.int
@@ -55,6 +63,25 @@ func VersionLib() Version {
5563
return Version{int(major), int(minor), int(patch), ""}
5664
}
5765

66+
// VersionLibStatic returns the Version of ObjectBox library this Go version was compiled against (build time);
67+
// see VersionLib() for the actually loaded version.
68+
// This version is at least VersionLibMinRecommended().
69+
func VersionLibStatic() Version {
70+
return Version{C.OBX_VERSION_MAJOR, C.OBX_VERSION_MINOR, C.OBX_VERSION_PATCH, ""}
71+
}
72+
73+
// VersionLibMin returns the minimum Version of the dynamic linked ObjectBox library that is compatible with this Go version
74+
func VersionLibMin() Version {
75+
return Version{0, 15, 0, ""}
76+
}
77+
78+
// VersionLibMinRecommended returns the minimum recommended Version of the dynamic linked ObjectBox library.
79+
// This version not only considers compatibility with this Go version, but also known issues older (compatible) versions.
80+
// It is guaranteed to be at least VersionLibMin()
81+
func VersionLibMinRecommended() Version {
82+
return Version{0, 15, 1, ""}
83+
}
84+
5885
// VersionInfo returns a printable version string
5986
func VersionInfo() string {
6087
return "ObjectBox Go version " + VersionGo().String() + " using dynamic library version " + VersionLib().String()

test/version_test.go

+41-6
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import (
2525
"github.com/objectbox/objectbox-go/test/assert"
2626
)
2727

28-
func TestVersion(t *testing.T) {
28+
func TestObjectBoxVersionString(t *testing.T) {
2929
versionInfo := objectbox.VersionInfo()
3030
t.Log(versionInfo)
3131

@@ -36,21 +36,56 @@ func TestVersion(t *testing.T) {
3636
if !format.MatchString(versionGoString) {
3737
t.Errorf("ObjectBox-Go version %v doesn't match expected regexp %v", versionGoString, format)
3838
}
39-
versionGoInt := versionGo.Major*10000 + versionGo.Minor*100 + versionGo.Patch
40-
assert.True(t, versionGoInt >= 10600) // Update with new releases (won't fail if forgotten)
41-
assert.True(t, versionGoInt < 20000) // Future next major release
4239

4340
versionLib := objectbox.VersionLib()
4441
versionLibString := versionLib.String()
4542
if !format.MatchString(versionGoString) {
4643
t.Errorf("ObjectBox-C version %v doesn't match expected regexp %v", versionLibString, format)
4744
}
45+
46+
assert.Eq(t, true, strings.Contains(versionInfo, versionGoString))
47+
assert.Eq(t, true, strings.Contains(versionInfo, versionLibString))
48+
}
49+
50+
func TestExpectedObjectBoxVersion(t *testing.T) {
51+
versionGo := objectbox.VersionGo()
52+
versionGoInt := versionGo.Major*10000 + versionGo.Minor*100 + versionGo.Patch
53+
assert.True(t, versionGoInt >= 10600) // Update with new releases (won't fail if forgotten)
54+
assert.True(t, versionGoInt < 20000) // Future next major release
55+
56+
versionLib := objectbox.VersionLib()
4857
versionLibInt := versionLib.Major*10000 + versionLib.Minor*100 + versionLib.Patch
4958
assert.True(t, versionLibInt >= 1501) // Update with new releases (won't fail if forgotten)
5059
assert.True(t, versionLibInt < 10000) // Future next major release
60+
}
5161

52-
assert.Eq(t, true, strings.Contains(versionInfo, versionGoString))
53-
assert.Eq(t, true, strings.Contains(versionInfo, versionLibString))
62+
func TestObjectBoxMinLibVersion(t *testing.T) {
63+
assert.True(t, objectbox.VersionLib().GreaterThanOrEqualTo(objectbox.VersionLibMin()))
64+
assert.True(t, objectbox.VersionLibMinRecommended().GreaterThanOrEqualTo(objectbox.VersionLibMin()))
65+
assert.True(t, objectbox.VersionLibStatic().GreaterThanOrEqualTo(objectbox.VersionLibMinRecommended()))
66+
}
67+
68+
func TestVersionAgainstZeros(t *testing.T) {
69+
zeros := objectbox.Version{Major: 0, Minor: 0, Patch: 0}
70+
71+
assert.True(t, zeros.LessThan(objectbox.VersionLibMin()))
72+
assert.True(t, zeros.LessThan(objectbox.VersionLibMinRecommended()))
73+
assert.True(t, zeros.LessThan(objectbox.VersionLib()))
74+
assert.True(t, zeros.LessThan(objectbox.VersionGo()))
75+
76+
assert.True(t, objectbox.VersionLibMin().GreaterThanOrEqualTo(zeros))
77+
assert.True(t, objectbox.VersionLibMinRecommended().GreaterThanOrEqualTo(zeros))
78+
assert.True(t, objectbox.VersionLib().GreaterThanOrEqualTo(zeros))
79+
assert.True(t, objectbox.VersionGo().GreaterThanOrEqualTo(zeros))
80+
}
81+
82+
func TestVersion(t *testing.T) {
83+
assert.True(t, objectbox.Version{Major: 0, Minor: 0, Patch: 0}.LessThan(objectbox.Version{Major: 0, Minor: 0, Patch: 1}))
84+
assert.True(t, objectbox.Version{Major: 0, Minor: 0, Patch: 1}.LessThan(objectbox.Version{Major: 0, Minor: 1, Patch: 0}))
85+
assert.True(t, objectbox.Version{Major: 0, Minor: 1, Patch: 1}.LessThan(objectbox.Version{Major: 1, Minor: 0, Patch: 0}))
86+
assert.True(t, objectbox.Version{Major: 0, Minor: 1, Patch: 0}.LessThan(objectbox.Version{Major: 0, Minor: 1, Patch: 1}))
87+
assert.True(t, objectbox.Version{Major: 1, Minor: 1, Patch: 0}.LessThan(objectbox.Version{Major: 1, Minor: 1, Patch: 1}))
88+
assert.True(t, objectbox.Version{Major: 1, Minor: 0, Patch: 1}.LessThan(objectbox.Version{Major: 1, Minor: 1, Patch: 1}))
5489
}
5590

5691
func TestVersionLabel(t *testing.T) {

0 commit comments

Comments
 (0)