本文整理匯總了Golang中github.com/fatih/structs.Map函數的典型用法代碼示例。如果您正苦於以下問題:Golang Map函數的具體用法?Golang Map怎麽用?Golang Map使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Map函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: SaveProxy
func (ps *ProxyStorage) SaveProxy(toSave *Proxy, update bool) (int, error) {
logger.Tracef("Saving proxy to storage: %s", toSave)
var query interface{}
queryString := fmt.Sprintf(`[{"eq" : "%s", "in" : ["Ip"] },{"eq" : "%n", "in" : ["Port"] }]`, toSave.Ip, toSave.Port)
json.Unmarshal([]byte(queryString), &query)
queryResult := make(map[int]struct{}) // query result (document IDs) goes into map keys
if err := db.EvalQuery(query, ps.proxyCol, &queryResult); err != nil {
panic(err)
}
if len(queryResult) > 0 {
logger.Tracef("Exists in database: %s", toSave)
if update {
logger.Tracef("Updating %s", toSave)
for k, _ := range queryResult {
//expecting single key
return k, ps.proxyCol.Update(k, structs.Map(toSave))
}
}
}
return ps.proxyCol.Insert(structs.Map(toSave))
}
示例2: CreateToken
func (m *Mongore) CreateToken(token account.Token) error {
key := fmt.Sprintf("%s: %s", token.Type, token.User.Email)
db.Cache.Set(key, nil, time.Duration(token.Expires)*time.Minute)
db.AddHCache(key, token.Expires, structs.Map(token))
db.Cache.Set(token.AccessToken, nil, time.Duration(token.Expires))
db.AddHCache(token.AccessToken, token.Expires, structs.Map(token.User))
return nil
}
示例3: securityGroupRuleSingle
func securityGroupRuleSingle(rule *osSecurityGroupRules.SecGroupRule) map[string]interface{} {
m := structs.Map(rule)
m["SecurityGroupID"] = m["SecGroupID"]
return m
}
示例4: pluckElement
// Function pluckElement is used to retrieve an array of just one field(destKeyName) present in original structure(plant).
// Input : 'plant' is the source from which an element needs to be plucked. An array of structure is expected.
// 'destKeyName' is the output element key name. This should not be an array as this will be used just to
// form the output structure.
// Output : []interface{} - An array is returned. Type assertion can be used to derive an array of required type.
// See example for more details.
func pluckElement(plant interface{}, destKeyName string) ([]interface{}, error) {
flag := 0
// Read the value from interface{}
srcExtract := reflect.ValueOf(plant)
// The result map[string]interface{} to be returned
var elementExtract []interface{}
// Retrieve the source elements one by one and copy to dest
for i := 0; i < srcExtract.Len(); i++ {
indexVal := srcExtract.Index(i).Interface()
indexValMap := structs.Map(indexVal)
if value, present := indexValMap[destKeyName]; present {
elementExtract = append(elementExtract, value)
flag = 1
}
}
// This is to make sure that at least one value got extracted from plant to branch
if flag == 0 {
err := errors.New("Source Destination Type Mismatch")
return nil, err
}
return elementExtract, nil
}
示例5: getUsers
func getUsers(user string, properties []string) {
api := setupAPI()
// Get the user from the Graph.
result, err := api.GetUser(user, properties)
if err != nil {
fmt.Fprintf(os.Stderr, "Error: %v", err)
return
}
// Convert the object to a map.
m := structs.Map(result)
// Get longest property name and sort all property names
// alphabetically.
var props []string
longest := 0
for k, _ := range m {
props = append(props, k)
if len(k) > longest {
longest = len(k)
}
}
sort.Strings(props)
// Write out the properties.
format := fmt.Sprintf("%%-%ds: %%v\n", longest)
for _, prop := range props {
fmt.Printf(format, prop, getValueAsString(m, prop))
}
}
示例6: main
func main() {
templateHtml, err := mustache.ParseFile("./report.html")
if err != nil {
log.Fatal("Parse Html failed, ", err)
}
var tmpfile = tmpDir + "test_report.html"
tmpHtmlFile, err := os.Create(tmpfile)
if err != nil {
log.Fatal("Create tmp file failed, ", err)
}
var data Data
data.Name = "!!! XXXX !!!!"
mapping := structs.Map(data)
var str = templateHtml.Render(mapping)
filelength, err := tmpHtmlFile.WriteString(str)
fmt.Printf("wrote %d bytes\n", filelength)
tmpHtmlFile.Sync()
cmd := "wkhtmltopdf"
args := []string{"--print-media-type", "--page-size", "A4", "-T", "0mm", "-B", "0mm", "-L", "0mm", "-R", "0mm", "--dpi", "600", "test_report.html", "test_report.pdf"}
if err := exec.Command(cmd, args...).Run(); err != nil {
fmt.Println(err)
os.Exit(1)
}
fmt.Println("Successfully Generates the report of PDF format")
}
示例7: handle
//handle takes a job and appropriate sends it to a function that will run it
func (qs *QueueService) handle(job *QueueJob) {
switch job.Job {
case "updateDatasetFile":
udfjd := UpdateDatasetFileJobData{}
jobJson, err := json.Marshal(job.Data)
if err != nil {
log.Fatal("Error creating JSON: ", err)
}
err = json.Unmarshal(jobJson, &udfjd)
if err != nil {
log.Fatal("Error parsing JSON: ", err)
}
/*
pds := NewPredictDatasetService(database.GetDatabase())
pds.UpdateFile(udfjd)
*/
fs := files.NewFileService(database.GetDatabase())
newTrans, err := fs.UpdateFile(udfjd.Extra.Subdom, udfjd.FileGUID, udfjd.TemplateGUID)
//Alert luminous that its Done
queue := NewQueueService("luminous")
jobData := structs.Map(udfjd)
jobData["NewTrans"] = newTrans
qj := &QueueJob{
Job: `Spark451\Luminous\v2\Predict\Classes\[email protected]`,
Data: jobData,
}
queue.Push(qj)
}
}
示例8: Write
// Write populates a buffer using a template with NGINX configuration
// and the servers and upstreams created by Ingress rules
func (t *Template) Write(cfg config.Configuration, ingressCfg ingress.Configuration) ([]byte, error) {
conf := make(map[string]interface{})
conf["backlogSize"] = sysctlSomaxconn()
conf["upstreams"] = ingressCfg.Upstreams
conf["servers"] = ingressCfg.Servers
conf["tcpUpstreams"] = ingressCfg.TCPUpstreams
conf["udpUpstreams"] = ingressCfg.UDPUpstreams
conf["defResolver"] = cfg.Resolver
conf["sslDHParam"] = cfg.SSLDHParam
conf["customErrors"] = len(cfg.CustomHTTPErrors) > 0
conf["cfg"] = fixKeyNames(structs.Map(cfg))
if glog.V(3) {
b, err := json.Marshal(conf)
if err != nil {
glog.Errorf("unexpected error: %v", err)
}
glog.Infof("NGINX configuration: %v", string(b))
}
buffer := new(bytes.Buffer)
err := t.tmpl.Execute(buffer, conf)
if err != nil {
glog.V(3).Infof("%v", string(buffer.Bytes()))
}
return buffer.Bytes(), err
}
示例9: prepContext
func (p *LuaPlugin) prepContext(L *lua.State, req *http.Request, ctx *apiplexy.APIContext) {
var clientIP string
if req.Header.Get("X-Forwarded-For") != "" {
clientIP = req.Header.Get("X-Forwarded-For")
} else {
clientIP, _, _ = net.SplitHostPort(req.RemoteAddr)
}
headers := make(map[string]interface{}, len(req.Header))
for k, vs := range req.Header {
headers[k] = strings.Join(vs, " ")
}
request := map[string]interface{}{
"path": req.URL.Path,
"method": req.Method,
"ip": clientIP,
"referrer": req.Referer(),
"browser": req.UserAgent(),
"headers": headers,
}
pushMap(L, request, false)
L.SetGlobal("request")
pushMap(L, structs.Map(ctx), true)
L.SetGlobal("context")
}
示例10: LoginUser
// These are the endpoints required to do a login and verifying that tokens are
// alive
func LoginUser(c *gin.Context) {
var user User
var loginer login.Loginer
var tokenStr string
var token Token
var tokener tokens.Tokener
c.BindJSON(&user)
loginer = user
tokener = token
SignatureStr, email := loginer.Login(user.Username, user.Password)
tokenStr, err := tokener.GenerateToken(SignatureStr)
if err != nil {
c.JSON(404, gin.H{"error generating token": err})
} else {
data := structs.Map(user)
err = boltdb.UpdateBucket(db, tokenStr, data)
if err != nil {
c.JSON(404, gin.H{"error updating bucket": err})
} else {
c.JSON(201, gin.H{"token": tokenStr, "email": email})
}
}
}
示例11: AndStoreMany
func (m *StoreModifier) AndStoreMany(objs []interface{}) *StoreModifier {
m.Request.Controls.Multiplicity = "multiple"
s := reflect.ValueOf(objs)
var interfaceList []map[string]interface{}
interfaceList = make([]map[string]interface{}, s.Len())
for i := 0; i < s.Len(); i++ {
//newMap := structs.Map(s.Index(i).Interface())
obj := s.Index(i).Interface()
v := reflect.ValueOf(obj)
k := v.Kind()
fmt.Println("KIND : ", k)
var newMap map[string]interface{}
if k != reflect.Map {
newMap = structs.Map(obj)
} else {
newMap = obj.(map[string]interface{})
}
interfaceList[i] = newMap
}
//for index, element := range objs {
// interfaceList[index] = structs.Map(element)
//}
m.Request.Body.Objects = interfaceList
return m
}
示例12: writeCfg
func (ngx *Manager) writeCfg(cfg nginxConfiguration, ingressCfg IngressConfig) (bool, error) {
conf := make(map[string]interface{})
conf["upstreams"] = ingressCfg.Upstreams
conf["servers"] = ingressCfg.Servers
conf["tcpUpstreams"] = ingressCfg.TCPUpstreams
conf["udpUpstreams"] = ingressCfg.UDPUpstreams
conf["defResolver"] = ngx.defResolver
conf["sslDHParam"] = ngx.sslDHParam
conf["cfg"] = fixKeyNames(structs.Map(cfg))
buffer := new(bytes.Buffer)
err := ngx.template.Execute(buffer, conf)
if err != nil {
glog.Infof("NGINX error: %v", err)
return false, err
}
if glog.V(3) {
b, err := json.Marshal(conf)
if err != nil {
fmt.Println("error:", err)
}
glog.Infof("NGINX configuration: %v", string(b))
}
changed, err := ngx.needsReload(buffer)
if err != nil {
return false, err
}
return changed, nil
}
示例13: AutoPathExt
// AutoPath returns a path based on the task name and parameters.
func AutoPathExt(outp Outputter, ext string) string {
directory := strings.Replace(fmt.Sprintf("%T", outp), ".", "/", -1)
m := structs.Map(outp)
var keys []string
for k := range m {
keys = append(keys, k)
}
sort.Strings(keys)
var parts []string
for _, k := range keys {
parts = append(parts, k)
value := m[k]
switch v := value.(type) {
case string:
parts = append(parts, v)
case fmt.Stringer:
parts = append(parts, v.String())
default:
parts = append(parts, fmt.Sprintf("%v", m[k]))
}
}
fn := strings.Join(parts, "-")
if len(fn) == 0 {
fn = "output"
}
filename := fn + "." + ext
return path.Join(directory, filename)
}
示例14: GetStats
func (app *Application) GetStats() map[string]interface{} {
statsInterface := app.Storage.Get("stats")
if statsInterface == nil {
return nil
}
stats := statsInterface.(*models.Stats)
payload := structs.Map(stats)
// Fetch the other stats data from file.
statsFromFileInterface := app.Storage.Get("statsFromFile")
if statsFromFileInterface != nil {
statsFromFile := statsFromFileInterface.(map[string]interface{})
for key, value := range statsFromFile {
trimmedKey := strings.Replace(key, "libmcrouter.mcrouter.5000.", "", -1)
payload[trimmedKey] = value
}
}
hostname, err := os.Hostname()
if err == nil {
payload["hostname"] = hostname
}
return payload
}
示例15: FindObjComments
// FindObjComments 獲得某個對象的所有評論
// owner: 被評論對象屬主
// TODO:分頁暫不做
func (self CommentLogic) FindObjComments(ctx context.Context, objid, objtype int, owner, lastCommentUid int /*, page, pageNum int*/) (comments []map[string]interface{}, ownerUser, lastReplyUser *model.User) {
objLog := GetLogger(ctx)
commentList := make([]*model.Comment, 0)
err := MasterDB.Where("objid=? AND objtype=?", objid, objtype).Find(&commentList)
if err != nil {
objLog.Errorln("CommentLogic FindObjComments Error:", err)
return
}
uids := slices.StructsIntSlice(commentList, "Uid")
// 避免某些情況下最後回複人沒在回複列表中
uids = append(uids, owner, lastCommentUid)
// 獲得用戶信息
userMap := DefaultUser.FindUserInfos(ctx, uids)
ownerUser = userMap[owner]
if lastCommentUid != 0 {
lastReplyUser = userMap[lastCommentUid]
}
comments = make([]map[string]interface{}, 0, len(commentList))
for _, comment := range commentList {
tmpMap := structs.Map(comment)
tmpMap["content"] = template.HTML(self.decodeCmtContent(ctx, comment))
tmpMap["user"] = userMap[comment.Uid]
comments = append(comments, tmpMap)
}
return
}