You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+26-2Lines changed: 26 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -90,7 +90,7 @@ func main() {
90
90
91
91
`gal` comes with pre-defined type interfaces: Numberer, Booler, Stringer (and maybe more in the future).
92
92
93
-
They allow the general use of types. For instance, the String `"123"` can be converted to the Number `123`.
93
+
They allow the use of general types. For instance, the String `"123"` can be converted to the Number `123`.
94
94
With `Numberer`, a user-defined function can transparently use String and Number when both hold a number representation.
95
95
96
96
A user-defined function can do this:
@@ -184,7 +184,7 @@ This allows parsing the expression once with `Parse` and run `Tree`.`Eval` multi
184
184
185
185
## Objects
186
186
187
-
Objects are Go `struct`'s which **properties**act as **gal variables** and **methods**as**gal functions**.
187
+
Objects are Go `struct`'s which **properties**behave similarly to **gal variables** and **methods**to**gal functions**.
188
188
189
189
Object definitions are passed as a `map[string]Object` using `WithObjects` when calling `Eval` from `Tree`.
190
190
@@ -220,6 +220,30 @@ Example:
220
220
221
221
```
222
222
223
+
## Objects Dot accessors
224
+
225
+
While user-defined Objects are generally Value-centric, `gal` supports accessing porperties and methods on Go objects too, using the `.` accessor.
226
+
227
+
Example:
228
+
229
+
`aCar.Stereo` returns a `CarStereo` struct. Its property `Brand` returns a `StereoBrand` that contains 2 properties `Name` and `Country`. None of these use `gal.Value` but the Dot accessor permits traversing them transparently.
230
+
231
+
`gal` will convert basic Go types such as `int` or `bool` to their `gal.Value` equivalent. This helps, at the end of the chain, to continue with the evaluation of the expression.
232
+
233
+
```go
234
+
expr:=`aCar.Stereo.Brand.Name`
235
+
```
236
+
237
+
Dot is an accessor. It can be thought of as a symbol. It is not an operator!
238
+
239
+
```go
240
+
// This is NOT a valid expression. While it may parse, it won't evaluate!
241
+
((aCar.Stereo).Brand).Country
242
+
243
+
// And of course, gal will refuse to evaluate this expression:
// Note: in this test, WithObjects is called with a `Car`, not a `*Car`.
588
588
// However, Car.CurrentSpeed has a *Car receiver, hence from a Go perspective, the method
589
589
// exists on *Car but it does NOT exist on Car!
590
-
assert.Equal(t, "undefined: error: object method 'aCar.CurrentSpeed': unknown or non-callable member (check if it has a pointer receiver)", got.String())
590
+
assert.Equal(t, "undefined: error: object 'aCar' method 'CurrentSpeed': unknown or non-callable member (check if it has a pointer receiver)", got.String())
0 commit comments