diff --git a/accessors.go b/accessors.go index af6cb5f..72f1d1c 100644 --- a/accessors.go +++ b/accessors.go @@ -37,11 +37,11 @@ var mapAccessRegex = regexp.MustCompile(mapAccessRegexString) // // Get can only operate directly on map[string]interface{} and []interface. // -// Example +// # Example // // To access the title of the third chapter of the second book, do: // -// o.Get("books[1].chapters[2].title") +// o.Get("books[1].chapters[2].title") func (m Map) Get(selector string) *Value { rawObj := access(m, selector, nil, false) return &Value{data: rawObj} @@ -52,11 +52,11 @@ func (m Map) Get(selector string) *Value { // // Set can only operate directly on map[string]interface{} and []interface // -// Example +// # Example // // To set the title of the third chapter of the second book, do: // -// o.Set("books[1].chapters[2].title","Time to Go") +// o.Set("books[1].chapters[2].title","Time to Go") func (m Map) Set(selector string, value interface{}) Map { access(m, selector, value, true) return m diff --git a/codegen/template.txt b/codegen/template.txt index af47531..9a5f6e1 100644 --- a/codegen/template.txt +++ b/codegen/template.txt @@ -1,5 +1,5 @@ /* - {4} ({1} and []{1}) +{4} ({1} and []{1}) */ // {4} gets the value as a {1}, returns the optionalDefault diff --git a/codegen/template_test.txt b/codegen/template_test.txt index e42d7fb..6b40d77 100644 --- a/codegen/template_test.txt +++ b/codegen/template_test.txt @@ -1,5 +1,5 @@ /* - Tests for {4} ({1} and []{1}) +Tests for {4} ({1} and []{1}) */ func Test{4}(t *testing.T) { val := {1}({2}) diff --git a/doc.go b/doc.go index 2c38c8f..b170af7 100644 --- a/doc.go +++ b/doc.go @@ -1,19 +1,19 @@ /* Package objx provides utilities for dealing with maps, slices, JSON and other data. -Overview +# Overview Objx provides the `objx.Map` type, which is a `map[string]interface{}` that exposes a powerful `Get` method (among others) that allows you to easily and quickly get access to data within the map, without having to worry too much about type assertions, missing data, default values etc. -Pattern +# Pattern Objx uses a predictable pattern to make access data from within `map[string]interface{}` easy. Call one of the `objx.` functions to create your `objx.Map` to get going: - m, err := objx.FromJSON(json) + m, err := objx.FromJSON(json) NOTE: Any methods or functions with the `Must` prefix will panic if something goes wrong, the rest will be optimistic and try to figure things out without panicking. @@ -21,46 +21,46 @@ the rest will be optimistic and try to figure things out without panicking. Use `Get` to access the value you're interested in. You can use dot and array notation too: - m.Get("places[0].latlng") + m.Get("places[0].latlng") Once you have sought the `Value` you're interested in, you can use the `Is*` methods to determine its type. - if m.Get("code").IsStr() { // Your code... } + if m.Get("code").IsStr() { // Your code... } Or you can just assume the type, and use one of the strong type methods to extract the real value: - m.Get("code").Int() + m.Get("code").Int() If there's no value there (or if it's the wrong type) then a default value will be returned, or you can be explicit about the default value. - Get("code").Int(-1) + Get("code").Int(-1) If you're dealing with a slice of data as a value, Objx provides many useful methods for iterating, manipulating and selecting that data. You can find out more by exploring the index below. -Reading data +# Reading data A simple example of how to use Objx: - // Use MustFromJSON to make an objx.Map from some JSON - m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`) + // Use MustFromJSON to make an objx.Map from some JSON + m := objx.MustFromJSON(`{"name": "Mat", "age": 30}`) - // Get the details - name := m.Get("name").Str() - age := m.Get("age").Int() + // Get the details + name := m.Get("name").Str() + age := m.Get("age").Int() - // Get their nickname (or use their name if they don't have one) - nickname := m.Get("nickname").Str(name) + // Get their nickname (or use their name if they don't have one) + nickname := m.Get("nickname").Str(name) -Ranging +# Ranging Since `objx.Map` is a `map[string]interface{}` you can treat it as such. For example, to `range` the data, do what you would expect: - m := objx.MustFromJSON(json) - for key, value := range m { - // Your code... - } + m := objx.MustFromJSON(json) + for key, value := range m { + // Your code... + } */ package objx diff --git a/map.go b/map.go index a64712a..ab9f9ae 100644 --- a/map.go +++ b/map.go @@ -47,17 +47,16 @@ func New(data interface{}) Map { // // The arguments follow a key, value pattern. // -// // Returns nil if any key argument is non-string or if there are an odd number of arguments. // -// Example +// # Example // // To easily create Maps: // -// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true)) +// m := objx.MSI("name", "Mat", "age", 29, "subobj", objx.MSI("active", true)) // -// // creates an Map equivalent to -// m := objx.Map{"name": "Mat", "age": 29, "subobj": objx.Map{"active": true}} +// // creates an Map equivalent to +// m := objx.Map{"name": "Mat", "age": 29, "subobj": objx.Map{"active": true}} func MSI(keyAndValuePairs ...interface{}) Map { newMap := Map{} keyAndValuePairsLen := len(keyAndValuePairs) diff --git a/type_specific_codegen_test.go b/type_specific_codegen_test.go index 72fa8c8..575aa85 100644 --- a/type_specific_codegen_test.go +++ b/type_specific_codegen_test.go @@ -9,7 +9,7 @@ import ( ) /* - Tests for Inter (interface{} and []interface{}) +Tests for Inter (interface{} and []interface{}) */ func TestInter(t *testing.T) { val := interface{}("something") @@ -130,7 +130,7 @@ func TestCollectInter(t *testing.T) { } /* - Tests for Bool (bool and []bool) +Tests for Bool (bool and []bool) */ func TestBool(t *testing.T) { val := bool(true) @@ -251,7 +251,7 @@ func TestCollectBool(t *testing.T) { } /* - Tests for Str (string and []string) +Tests for Str (string and []string) */ func TestStr(t *testing.T) { val := string("hello") @@ -372,7 +372,7 @@ func TestCollectStr(t *testing.T) { } /* - Tests for Int (int and []int) +Tests for Int (int and []int) */ func TestInt(t *testing.T) { val := int(1) @@ -493,7 +493,7 @@ func TestCollectInt(t *testing.T) { } /* - Tests for Int8 (int8 and []int8) +Tests for Int8 (int8 and []int8) */ func TestInt8(t *testing.T) { val := int8(1) @@ -614,7 +614,7 @@ func TestCollectInt8(t *testing.T) { } /* - Tests for Int16 (int16 and []int16) +Tests for Int16 (int16 and []int16) */ func TestInt16(t *testing.T) { val := int16(1) @@ -735,7 +735,7 @@ func TestCollectInt16(t *testing.T) { } /* - Tests for Int32 (int32 and []int32) +Tests for Int32 (int32 and []int32) */ func TestInt32(t *testing.T) { val := int32(1) @@ -856,7 +856,7 @@ func TestCollectInt32(t *testing.T) { } /* - Tests for Int64 (int64 and []int64) +Tests for Int64 (int64 and []int64) */ func TestInt64(t *testing.T) { val := int64(1) @@ -977,7 +977,7 @@ func TestCollectInt64(t *testing.T) { } /* - Tests for Uint (uint and []uint) +Tests for Uint (uint and []uint) */ func TestUint(t *testing.T) { val := uint(1) @@ -1098,7 +1098,7 @@ func TestCollectUint(t *testing.T) { } /* - Tests for Uint8 (uint8 and []uint8) +Tests for Uint8 (uint8 and []uint8) */ func TestUint8(t *testing.T) { val := uint8(1) @@ -1219,7 +1219,7 @@ func TestCollectUint8(t *testing.T) { } /* - Tests for Uint16 (uint16 and []uint16) +Tests for Uint16 (uint16 and []uint16) */ func TestUint16(t *testing.T) { val := uint16(1) @@ -1340,7 +1340,7 @@ func TestCollectUint16(t *testing.T) { } /* - Tests for Uint32 (uint32 and []uint32) +Tests for Uint32 (uint32 and []uint32) */ func TestUint32(t *testing.T) { val := uint32(1) @@ -1461,7 +1461,7 @@ func TestCollectUint32(t *testing.T) { } /* - Tests for Uint64 (uint64 and []uint64) +Tests for Uint64 (uint64 and []uint64) */ func TestUint64(t *testing.T) { val := uint64(1) @@ -1582,7 +1582,7 @@ func TestCollectUint64(t *testing.T) { } /* - Tests for Uintptr (uintptr and []uintptr) +Tests for Uintptr (uintptr and []uintptr) */ func TestUintptr(t *testing.T) { val := uintptr(1) @@ -1703,7 +1703,7 @@ func TestCollectUintptr(t *testing.T) { } /* - Tests for Float32 (float32 and []float32) +Tests for Float32 (float32 and []float32) */ func TestFloat32(t *testing.T) { val := float32(1) @@ -1824,7 +1824,7 @@ func TestCollectFloat32(t *testing.T) { } /* - Tests for Float64 (float64 and []float64) +Tests for Float64 (float64 and []float64) */ func TestFloat64(t *testing.T) { val := float64(1) @@ -1945,7 +1945,7 @@ func TestCollectFloat64(t *testing.T) { } /* - Tests for Complex64 (complex64 and []complex64) +Tests for Complex64 (complex64 and []complex64) */ func TestComplex64(t *testing.T) { val := complex64(1) @@ -2066,7 +2066,7 @@ func TestCollectComplex64(t *testing.T) { } /* - Tests for Complex128 (complex128 and []complex128) +Tests for Complex128 (complex128 and []complex128) */ func TestComplex128(t *testing.T) { val := complex128(1) diff --git a/type_specific_test.go b/type_specific_test.go index 80592fe..505e19a 100644 --- a/type_specific_test.go +++ b/type_specific_test.go @@ -9,7 +9,7 @@ import ( ) /* - Tests for MSI (map[string]interface{} and []map[string]interface{}) +Tests for MSI (map[string]interface{} and []map[string]interface{}) */ func TestMSI(t *testing.T) { val := map[string]interface{}(map[string]interface{}{"name": "Tyler"}) @@ -232,7 +232,7 @@ func TestCollectMSI2(t *testing.T) { } /* - Tests for ObjxMap ((objx.Map) and [](objx.Map)) +Tests for ObjxMap ((objx.Map) and [](objx.Map)) */ func TestObjxMap(t *testing.T) { val := (objx.Map)(objx.New(1))