-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
129 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package ubjson_test | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/jmank88/ubjson" | ||
) | ||
|
||
// A CustomValue encodes itself as a fixed length object container. | ||
type TaggedStruct struct { | ||
Field1 string `ubjson:"field1"` | ||
FieldA int `json:"ignored" ubjson:"fieldA"` | ||
} | ||
|
||
func Example_taggedStruct() { | ||
v := &TaggedStruct{Field1: "test", FieldA: 42} | ||
if b, err := ubjson.MarshalBlock(v); err != nil { | ||
fmt.Println("error: " + err.Error()) | ||
} else { | ||
fmt.Println(string(b)) | ||
} | ||
|
||
// Output: | ||
// [{] | ||
// [U][6][field1][S][U][4][test] | ||
// [U][6][fieldA][U][42] | ||
// [}] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package ubjson | ||
|
||
import ( | ||
"reflect" | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// Based on 'encoding/json/encode.go'. | ||
var fieldCache struct { | ||
value atomic.Value // map[reflect.Type]fields | ||
mu sync.Mutex // used only by writers | ||
} | ||
|
||
// cachedTypeFields is like typeFields but uses a cache to avoid repeated work. | ||
// Based on 'encoding/json/encode.go'. | ||
func cachedTypeFields(t reflect.Type) fields { | ||
m, _ := fieldCache.value.Load().(map[reflect.Type]fields) | ||
f, ok := m[t] | ||
if ok { | ||
return f | ||
} | ||
|
||
// Compute names without lock. | ||
// Might duplicate effort but won't hold other computations back. | ||
f = typeFields(t) | ||
|
||
fieldCache.mu.Lock() | ||
m, _ = fieldCache.value.Load().(map[reflect.Type]fields) | ||
newM := make(map[reflect.Type]fields, len(m)+1) | ||
for k, v := range m { | ||
newM[k] = v | ||
} | ||
newM[t] = f | ||
fieldCache.value.Store(newM) | ||
fieldCache.mu.Unlock() | ||
return f | ||
} | ||
|
||
// Indexes fields by 'ubjson' struct tag if present, otherwise name. | ||
func typeFields(t reflect.Type) fields { | ||
fs := fields{ | ||
indexByName: make(map[string]int), | ||
} | ||
for i := 0; i < t.NumField(); i++ { | ||
f := t.Field(i) | ||
if f.PkgPath == "" { | ||
name := f.Name | ||
// Check for 'ubjson' struct tag. | ||
if v, ok := f.Tag.Lookup("ubjson"); ok { | ||
name = v | ||
} | ||
fs.names = append(fs.names, name) | ||
fs.indexByName[name] = i | ||
} | ||
} | ||
return fs | ||
} | ||
|
||
type fields struct { | ||
names []string | ||
indexByName map[string]int | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters