Skip to content

Commit a2a3fc4

Browse files
committed
Added UCL Object construction functions
These functions wrap around libucl's ucl_object_from* functions. Additional tests were added to the test suite in object_test.go to test that these new functions operate correctly.
1 parent 39dbadb commit a2a3fc4

File tree

2 files changed

+133
-5
lines changed

2 files changed

+133
-5
lines changed

object.go

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -217,15 +217,49 @@ const (
217217
StringParseBoolean StringFlag = C.UCL_STRING_PARSE_BOOLEAN
218218
// StringParseInt tells the converter to parse the inputted string as an integer
219219
StringParseInt StringFlag = C.UCL_STRING_PARSE_INT
220-
// StringParseDouble tells the converter to parse the inputted string as a floating-point number
220+
// StringParseDouble tells the converter to parse the inputted string as a
221+
// floating-point number
221222
StringParseDouble StringFlag = C.UCL_STRING_PARSE_DOUBLE
222-
// StringParseTime tells the converter to parse the inputted string as a time value, and treat as a floating-point number.
223+
// StringParseTime tells the converter to parse the inputted string as a time value,
224+
// and treat as a floating-point number.
223225
StringParseTime StringFlag = C.UCL_STRING_PARSE_TIME
224-
// StringParseNumber tells the converter to parse the inputted string as a number (integer, floating-point or time)
226+
// StringParseNumber tells the converter to parse the inputted string as a number
227+
// (integer, floating-point or time)
225228
StringParseNumber StringFlag = C.UCL_STRING_PARSE_TIME
226229
// StringParse tells the converter to parse the inputted string
227230
StringParse StringFlag = C.UCL_STRING_PARSE
228-
// StringParseBytes tells the converter to parse the inputted string as being in bytes notation
229-
// (e.g. 10k = 10*1024, not 10*1000)
231+
// StringParseBytes tells the converter to parse the inputted string as being in
232+
// bytes notation (e.g. 10k = 10*1024, not 10*1000)
230233
StringParseBytes StringFlag = C.UCL_STRING_PARSE_BYTES
231234
)
235+
236+
// NewObject creates a new UCL Object from a string, JSON escaping it in the process
237+
func NewObject(data string) *Object {
238+
obj := C.ucl_object_fromlstring(C.CString(data), C.size_t(len(data)))
239+
return &Object{object: obj}
240+
}
241+
242+
// NewFormattedObject creates a new UCL Object from a string, according to the instructions
243+
// given in the flags
244+
func NewFormattedObject(data string, flags StringFlag) *Object {
245+
obj := C.ucl_object_fromstring_common(C.CString(data), C.size_t(len(data)), uint32(flags))
246+
return &Object{object: obj}
247+
}
248+
249+
// NewIntegerObject creates a new UCL Object from a 64-bit integer
250+
func NewIntegerObject(data int64) *Object {
251+
obj := C.ucl_object_fromint(C.int64_t(data))
252+
return &Object{object: obj}
253+
}
254+
255+
// NewDoubleObject creates a new UCL Object from a 64-bit floating-point number
256+
func NewDoubleObject(data float64) *Object {
257+
obj := C.ucl_object_fromdouble(C.double(data))
258+
return &Object{object: obj}
259+
}
260+
261+
// NewBoolObject creates a new UCL Object from a boolean
262+
func NewBoolObject(data bool) *Object {
263+
obj := C.ucl_object_frombool(C.bool(data))
264+
return &Object{object: obj}
265+
}

object_test.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,97 @@ func TestObjectToFloat_negativeOneThird(t *testing.T) {
234234
t.Fatalf("bad: %#v, expected: %v", v.ToFloat(), g)
235235
}
236236
}
237+
238+
func TestIntToObject(t *testing.T) {
239+
var testValue int64 = 42
240+
obj := NewIntegerObject(testValue)
241+
defer obj.Close()
242+
v := obj.ToInt()
243+
if v != testValue {
244+
t.Fatalf("bad: %d, expected: %d", v, testValue)
245+
}
246+
}
247+
248+
func TestBoolToObject(t *testing.T) {
249+
objTrue := NewBoolObject(true)
250+
defer objTrue.Close()
251+
if !objTrue.ToBool() {
252+
t.Fatalf("bad: false, expected: true")
253+
}
254+
objFalse := NewBoolObject(false)
255+
defer objFalse.Close()
256+
if objFalse.ToBool() {
257+
t.Fatalf("bad: true, expected: false")
258+
}
259+
}
260+
261+
func TestFloatToObject(t *testing.T) {
262+
testValue := -1.0 / 3.0
263+
obj := NewDoubleObject(testValue)
264+
defer obj.Close()
265+
if obj.ToFloat() != testValue {
266+
t.Fatalf("bad: %#v, expected: %v", obj.ToFloat(), testValue)
267+
}
268+
}
269+
270+
func TestSimpleStringToObject(t *testing.T) {
271+
s := "Hello World!"
272+
obj := NewObject(s)
273+
defer obj.Close()
274+
if obj.ToString() != s {
275+
t.Fatalf("bad: \"%s\", expected: \"%s\"", obj.ToString(), s)
276+
}
277+
}
278+
279+
func TestJSONEscapeStringToObject(t *testing.T) {
280+
inputString := "complex\tstring\nfrom\"hell\""
281+
escapedString := "complex\\tstring\\nfrom\\\"hell\\\""
282+
obj := NewObject(inputString)
283+
defer obj.Close()
284+
if obj.ToString() != escapedString {
285+
t.Fatalf("bad: \"%s\", expected: \"%s\"", obj.ToString(), escapedString)
286+
}
287+
}
288+
289+
func TestStringIntToObject(t *testing.T) {
290+
var i int64 = 42
291+
s := "42"
292+
obj := NewFormattedObject(s, StringParseInt)
293+
defer obj.Close()
294+
if obj.ToInt() != i {
295+
t.Fatalf("bad: %d, expected: %d", obj.ToInt(), i)
296+
}
297+
}
298+
299+
func TestStringFloatToObject(t *testing.T) {
300+
expectedValue := -1.0 / 3.0
301+
testString := "-0.3333333333333333148296162562473909929394721984863281"
302+
obj := NewFormattedObject(testString, StringParseDouble)
303+
defer obj.Close()
304+
if obj.ToFloat() != expectedValue {
305+
t.Fatalf("bad: %#v, expected: %v", obj.ToFloat(), expectedValue)
306+
}
307+
}
308+
309+
func TestStringBoolToObject(t *testing.T) {
310+
objFalse := NewFormattedObject("false", StringParseBoolean)
311+
defer objFalse.Close()
312+
if objFalse.ToBool() {
313+
t.Fatal("bad: true, expected: false")
314+
}
315+
objTrue := NewFormattedObject("true", StringParseBoolean)
316+
defer objTrue.Close()
317+
if !objTrue.ToBool() {
318+
t.Fatal("bad: false, expected: true")
319+
}
320+
}
321+
322+
func TestStringTrimToObject(t *testing.T) {
323+
rawString := " Hello World\n\t\n\t\n\t "
324+
expectedResult := "Hello World"
325+
obj := NewFormattedObject(rawString, StringTrim)
326+
defer obj.Close()
327+
if obj.ToString() != expectedResult {
328+
t.Fatalf("bad: \"%s\", expected: \"%s\"", obj.ToString(), expectedResult)
329+
}
330+
}

0 commit comments

Comments
 (0)