Exemplo n.º 1
0
func ExampleKeyBy() {
	type Order struct {
		ID       int
		Shipping string
		Total    int
	}
	var keyOrdersById func([]Order, func(Order) int) map[int]Order
	if err := funcs.MakeKeyBy(&keyOrdersById); err != nil {
		log.Fatal(err)
	}
	orders := []Order{{1, "USA", 100}, {20, "Japan", 1000}, {60, "Spain", 250}}
	ordersById := keyOrdersById(orders, func(order Order) int {
		return order.ID
	})
	fmt.Println(ordersById[1])
	fmt.Println(ordersById[60])
	// Output:
	// {1 USA 100}
	// {60 Spain 250}
}
Exemplo n.º 2
0
	result := "metadata : {"
	result += "collectionName: '" + meta.targetDocument + "', "
	result += "idField: '" + meta.idField + "' "
	result += "fields :[\n"
	for i, field := range meta.fields {
		if i > 0 {
			result += ",\n "
		}
		result += "{" + field.String() + "}"
	}
	return result + "\n]}\n"
}

var (
	keyValuesByObjectID func(collection []reflect.Value, selector func(reflect.Value) bson.ObjectId) map[bson.ObjectId]reflect.Value
	_                   = funcs.Must(funcs.MakeKeyBy(&keyValuesByObjectID))

	getKeys func(map[bson.ObjectId]reflect.Value) []bson.ObjectId
	_       = funcs.Must(funcs.MakeGetKeys(&getKeys))

	flatten func([][]interface{}) []interface{}
	_       = funcs.Must(funcs.MakeFlatten(&flatten))

	mapResultsToInterfaces func([]map[string]interface{}, func(map[string]interface{}) []interface{}) [][]interface{}
	_                      = funcs.Must(funcs.MakeMap(&mapResultsToInterfaces))

	keyResultsBySourceID func(results []map[string]interface{}, mapper func(result map[string]interface{}) (id bson.ObjectId)) map[bson.ObjectId]map[string]interface{}
	_                    = funcs.Must(funcs.MakeKeyBy(&keyResultsBySourceID))

	mapResultsToRelatedObjectIds func(results []map[string]interface{}, mapper func(result map[string]interface{}) bson.ObjectId) []bson.ObjectId
	_                            = funcs.Must(funcs.MakeMap(&mapResultsToRelatedObjectIds))