Skip to content

Commit bd1b373

Browse files
committed
Verify external type to be known ones
1 parent 842e0b7 commit bd1b373

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

internal/generator/binding/field.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,12 @@ func (field *Field) ProcessAnnotations(a map[string]*Annotation) error {
125125
field.ModelProperty.ExternalName = a["external-name"].Value
126126
}
127127
if a["external-type"] != nil {
128-
field.ModelProperty.ExternalType = model.ExternalTypeValues[a["external-type"].Value]
128+
externalTypeValue := a["external-type"].Value
129+
externalType, exists := model.ExternalTypeValues[externalTypeValue]
130+
if !exists {
131+
return fmt.Errorf("invalid external-type '%s' for property %s - must be one of the supported external types", externalTypeValue, field.Name)
132+
}
133+
field.ModelProperty.ExternalType = externalType
129134
}
130135

131136
if a["index"] != nil {

internal/generator/binding/object.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,12 @@ func (object *Object) AddRelation(details map[string]*Annotation) (*model.Standa
140140
}
141141

142142
if details["external-type"] != nil {
143-
relation.ExternalType = model.ExternalTypeValues[details["external-type"].Value]
143+
externalTypeValue := details["external-type"].Value
144+
externalType, exists := model.ExternalTypeValues[externalTypeValue]
145+
if !exists {
146+
return nil, fmt.Errorf("invalid external-type '%s' for relation %s - must be one of the supported external types", externalTypeValue, relation.Name)
147+
}
148+
relation.ExternalType = externalType
144149
}
145150

146151
// NOTE: we don't need an actual entity pointer, it's resolved during stored model merging.

test/comparison/test-all.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,16 @@ func generateAllFiles(t *testing.T, overwriteExpected bool, conf testSpec, srcDi
255255
if err == nil {
256256
assert.Failf(t, "Unexpected PASS on a negative test %s", sourceFile)
257257
} else {
258-
var errPlatformIndependent = strings.Replace(err.Error(), "\\", "/", -1)
259-
assert.Eq(t, getExpectedError(t, sourceFile).Error(), errPlatformIndependent)
258+
var unifiedError = strings.Replace(err.Error(), "\\", "/", -1) // "Unify" Windows paths
259+
expectedError := getExpectedError(t, sourceFile).Error()
260+
if strings.HasPrefix(unifiedError, "error generating model from schema ") {
261+
// Compare only the last part of unifiedError as it contains the full path to the schema file
262+
unifiedError = unifiedError[len(unifiedError)-len(expectedError):]
263+
if unifiedError != expectedError {
264+
t.Logf("Full error: %s", err) // Initial error, which may contain additional information
265+
}
266+
}
267+
assert.Eq(t, expectedError, unifiedError)
260268
continue
261269
}
262270
} else {
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// ERROR = object 0 test.EntityWithInvalidExternalType: field 0 data: invalid external-type 'InvalidTypeName' for property data - must be one of the supported external types
2+
namespace test;
3+
4+
table EntityWithInvalidExternalType {
5+
id: ulong;
6+
/// objectbox: external-type=InvalidTypeName
7+
data: string;
8+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// ERROR = invalid external-type 'NonExistentType' for relation children - must be one of the supported external types
2+
namespace test;
3+
4+
table Child {
5+
id: ulong;
6+
text: string;
7+
}
8+
9+
/// objectbox:relation(name=children, to=Child, external-type=NonExistentType)
10+
table Parent {
11+
id: ulong;
12+
}

0 commit comments

Comments
 (0)