本文整理匯總了Golang中github.com/eaciit/toolkit.Unjson函數的典型用法代碼示例。如果您正苦於以下問題:Golang Unjson函數的具體用法?Golang Unjson怎麽用?Golang Unjson使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Unjson函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: OpenSession
func (c *Connection) OpenSession() error {
c.Close()
t, e := os.OpenFile(c.filePath, os.O_RDWR, 0)
if e != nil {
return errorlib.Error(packageName, modConnection, "Open File", e.Error())
}
c.openFile = t
i, e := ioutil.ReadFile(c.filePath)
if e != nil {
return errorlib.Error(packageName, modQuery+".Exec", "Read file", e.Error())
}
var hoomanJson []toolkit.M
e = toolkit.Unjson(i, &hoomanJson)
if e != nil {
return errorlib.Error(packageName, modQuery+".Exec", "Cannot Unjson", e.Error())
}
c.lines = 0
c.sData = ""
c.sameId = false
if len(hoomanJson) == 0 {
c.isNewSave = true
} else {
c.getJsonToMap = hoomanJson
}
return nil
}
示例2: ResultFromDatabase
func (g *GetDatabase) ResultFromDatabase(dataSettingId string, out interface{}) error {
c, e := dbox.NewConnection(g.desttype, &g.ConnectionInfo)
if e != nil {
return e
}
e = c.Connect()
if e != nil {
return e
}
defer c.Close()
iQ := c.NewQuery()
if g.CollectionSettings[dataSettingId].Collection != "" {
iQ.From(g.CollectionSettings[dataSettingId].Collection)
}
for _, val := range g.CollectionSettings[dataSettingId].MapsColumns {
iQ.Select(val.Source)
}
if len(g.CollectionSettings[dataSettingId].FilterCond) > 0 {
iQ.Where(g.CollectionSettings[dataSettingId].filterDbox)
}
csr, e := iQ.Cursor(nil)
if e != nil {
return e
}
if csr == nil {
return e
}
defer csr.Close()
results := make([]toolkit.M, 0)
e = csr.Fetch(&results, 0, false)
if e != nil {
return e
}
ms := []toolkit.M{}
for _, val := range results {
m := toolkit.M{}
for _, column := range g.CollectionSettings[dataSettingId].MapsColumns {
m.Set(column.Source, "")
if val.Has(column.Destination) {
m.Set(column.Source, val[column.Destination])
}
}
ms = append(ms, m)
}
if edecode := toolkit.Unjson(toolkit.Jsonify(ms), out); edecode != nil {
return edecode
}
return nil
}
示例3: TestDelete
func TestDelete(t *testing.T) {
//t.Skip()
ctx, e := prepareContext()
if e != nil {
t.Errorf("Error Connect: %s", e.Error())
return
}
defer ctx.Close()
u := new(UserModel)
e = ctx.GetById(u, "user2")
if e == nil {
fmt.Printf("Will Delete UserModel:\n %s \n", tk.JsonString(u))
e = ctx.Delete(u)
if e != nil {
t.Errorf("Error Load: %s", e.Error())
return
} else {
tk.Unjson(tk.Jsonify(u), u)
fmt.Printf("UserModel: %v has been deleted \n", u.RandomDate.UTC())
fmt.Println("")
}
} else {
t.Errorf("Delete error: %s", e.Error())
}
}
示例4: getMsgAsResult
func (s *Subscriber) getMsgAsResult(key string) *toolkit.Result {
result := toolkit.NewResult()
if key == "" {
if len(s.messageKeys) > 0 {
key = s.messageKeys[0]
}
}
// if no key is provided, check from the latest
if key == "" {
result.Status = toolkit.Status_NOK
result.Message = "No key has been provided to receive the message"
} else {
msgQue, exist := s.MessageQues[key]
if !exist {
result.Status = toolkit.Status_NOK
result.Message = "Key " + key + " is not exist on message que or it has been collected. Available keys are: " + strings.Join(s.messageKeys, ",")
} else {
url := fmt.Sprintf("%s/getmsg", s.BroadcasterAddress)
r, e := toolkit.HttpCall(url, "POST",
toolkit.Jsonify(msgQue), nil)
if e != nil {
result.SetErrorTxt("Subscriber ReceiveMsg Call Error: " + e.Error())
} else if r.StatusCode != 200 {
result.SetErrorTxt("Subsciber ReceiveMsg Call Error: " + r.Status)
} else {
//var resultMsg toolkit.Result
e := toolkit.Unjson(toolkit.HttpContent(r),
&result)
if e != nil {
result.SetErrorTxt(fmt.Sprintf("Subsciber ReceiveMsg Decode Error: ", e.Error()))
}
/*else {
if resultMsg.Status == toolkit.Status_OK {
result.Data = resultMsg.Data
} else {
result.SetErrorTxt(resultMsg.Message)
}
}
*/
}
}
}
if result.Status == "OK" {
url := fmt.Sprintf("%s/msgreceived")
toolkit.HttpCall(url, "POST", toolkit.Jsonify(struct {
Key string
Subscriber string
}{key, s.Address}), nil)
}
return result
}
示例5: GetJsonFile
func GetJsonFile(pathfile string) (toolkit.Ms, error) {
content, err := ioutil.ReadFile(pathfile)
if err != nil {
return nil, err
}
result := toolkit.Ms{}
if err := toolkit.Unjson(content, &result); err != nil {
return nil, err
}
return result, nil
}
示例6: distributeBroadcast
func (m *MessageMonitor) distributeBroadcast() {
for len(m.Targets) != m.Success && time.Now().After(m.Expiry) == false {
wg := new(sync.WaitGroup)
for k, t := range m.Targets {
wg.Add(1)
go func(wg *sync.WaitGroup, k int, t string) {
defer wg.Done()
if m.Status[k] != "OK" && m.retry[k] < RetryWhenFail() {
var command, url string
/*
if m.Command != "" {
command = m.Command
} else {
command = "msg"
}
*/
sub := m.broadcaster.Subscibers[t]
if sub != nil {
command = "pushmessage"
url = fmt.Sprintf("http://%s/subscriber/%s", t, command)
r, ecall := toolkit.HttpCall(url, "POST",
toolkit.M{}.Set("secret", sub.Secret).Set("Data", m.Data).Set("Key", m.Key).ToBytes("json", nil),
nil)
if ecall != nil {
m.setSuccessFail(k, "CALL ERROR: "+url+" ERR:"+ecall.Error())
} else if r.StatusCode != 200 {
m.setSuccessFail(k, fmt.Sprintf("CALL STATUS ERROR: %s ERR: %s", url, r.Status))
} else {
var result toolkit.Result
bs := toolkit.HttpContent(r)
edecode := toolkit.Unjson(bs, &result)
if edecode != nil {
m.setSuccessFail(k, "DECODE ERROR: "+string(bs)+" ERR:"+edecode.Error())
} else {
m.setSuccessFail(k, toolkit.IfEq(result.Status, toolkit.Status_OK, "OK", result.Message).(string))
}
}
}
}
}(wg, k, t)
}
wg.Wait()
//time.Sleep(1 * time.Second)
//fmt.Printf("%d = %d \n", len(m.Targets), m.Success+m.Fail)
}
}
示例7: parseHostAlias
func parseHostAlias(what string, raw interface{}) string {
hostAliases := []struct {
IP string `json:"ip", bson:"ip"`
HostName string `json:"hostName", bson:"hostName"`
}{}
toolkit.Unjson(toolkit.Jsonify(raw), &hostAliases)
for _, alias := range hostAliases {
if strings.Contains(strings.Split(what, "webhdfs")[0], alias.HostName) {
what = strings.Replace(what, alias.HostName, alias.IP, 1)
break
}
}
return what
}
示例8: ExtractFile
func (w *Widget) ExtractFile(compressedSource string, fileName string) error {
compressedFile := filepath.Join(compressedSource, fileName)
extractDest := filepath.Join(compressedSource, w.ID)
if err := os.RemoveAll(extractDest); err != nil {
return err
}
if strings.Contains(fileName, ".tar.gz") {
if err := toolkit.TarGzExtract(compressedFile, extractDest); err != nil {
return err
}
} else if strings.Contains(fileName, ".gz") {
if err := toolkit.GzExtract(compressedFile, extractDest); err != nil {
return err
}
} else if strings.Contains(fileName, ".tar") {
if err := toolkit.TarExtract(compressedFile, extractDest); err != nil {
return err
}
} else if strings.Contains(fileName, ".zip") {
if err := toolkit.ZipExtract(compressedFile, extractDest); err != nil {
return err
}
}
if err := os.Remove(compressedFile); err != nil {
return err
}
getConfigFile := filepath.Join(extractDest, "config.json")
content, err := ioutil.ReadFile(getConfigFile)
if err != nil {
return err
}
// contentstring := string(content)
var result = Config{}.data
toolkit.Unjson(content, &result)
toolkit.Println(result, string(content))
// checkDir(compressedSource, extractDest, w.ID)
return nil
}
示例9: ResultFromHtml
func (g *Grabber) ResultFromHtml(dataSettingId string, out interface{}) error {
reader := bytes.NewReader(g.bodyByte)
doc, e := gq.NewDocumentFromReader(reader)
if e != nil {
return e
}
ms := []toolkit.M{}
records := doc.Find(g.Config.DataSettings[dataSettingId].RowSelector)
recordCount := records.Length()
for i := 0; i < recordCount; i++ {
record := records.Eq(i)
m := toolkit.M{}
for cindex, c := range g.Config.DataSettings[dataSettingId].ColumnSettings {
columnId := fmt.Sprintf("%s", cindex)
if c.Alias != "" {
columnId = c.Alias
}
sel := record.Find(c.Selector)
var value interface{}
valuetype := strings.ToLower(c.ValueType)
if valuetype == "attr" {
value, _ = sel.Attr(c.AttrName)
} else if valuetype == "html" {
value, _ = sel.Html()
} else {
value = sel.Text()
}
value = strings.TrimSpace(fmt.Sprintf("%s", value))
m.Set(columnId, value)
}
if g.Config.DataSettings[dataSettingId].getCondition(m) {
ms = append(ms, m)
}
}
if edecode := toolkit.Unjson(toolkit.Jsonify(ms), out); edecode != nil {
return edecode
}
return nil
}
示例10: AddChannel
func (s *Subscriber) AddChannel(c string) error {
url := fmt.Sprintf("%s/channelregister", s.BroadcasterAddress)
r, e := toolkit.HttpCall(url, "POST", toolkit.Jsonify(ChannelRegister{c, s.Address}), nil)
if e != nil {
return fmt.Errorf("Channel Register Call Error: %s", e.Error())
}
if r.StatusCode != 200 {
return fmt.Errorf("Channel Register Call Error: %s", r.Status)
}
result := new(toolkit.Result)
e = toolkit.Unjson(toolkit.HttpContent(r), &result)
if e != nil {
return fmt.Errorf("Channel Register Decode error: %s", e.Error())
}
if result.Status != toolkit.Status_OK {
return fmt.Errorf("Channel Register error: %s", result.Message)
}
return nil
}
示例11: call
func call(url, call string, data []byte, expectedStatus int) (*toolkit.Result, error) {
cfg := toolkit.M{}
if expectedStatus != 0 {
cfg.Set("expectedstatus", expectedStatus)
}
r, e := toolkit.HttpCall(url, call, data, cfg)
if e != nil {
return nil, fmt.Errorf(url + " Call Error: " + e.Error())
}
result := toolkit.NewResult()
bs := toolkit.HttpContent(r)
edecode := toolkit.Unjson(bs, result)
if edecode != nil {
return nil, fmt.Errorf(url + " Http Result Decode Error: " + edecode.Error() + "\nFollowing got: " + string(bs))
}
if result.Status == toolkit.Status_NOK {
return nil, fmt.Errorf(url + " Http Result Error: " + result.Message)
}
return result, nil
}
示例12: GetForms
func (r *WebContext) GetForms(result interface{}) error {
if r.Request == nil {
return errors.New("HttpRequest object is not properly setup")
}
m := toolkit.M{}
e := r.Request.ParseForm()
if e != nil {
return e
}
for k, v := range r.Request.Form {
//fmt.Println("Receiving form %s : %v \n", k, v)
if f, floatOk := toolkit.StringToFloat(v[0]); floatOk {
m.Set(k, f)
} else {
m.Set(k, v[0])
}
}
e = toolkit.Unjson(m.ToBytes("json", nil), result)
return e
}
示例13: ditributeBroadcast
func (m *MessageMonitor) ditributeBroadcast() {
//for len(m.Targets) != m.Success && time.Now().After(m.Expiry) == false {
wg := new(sync.WaitGroup)
for k, t := range m.Targets {
wg.Add(1)
go func(wg *sync.WaitGroup, k int, t string) {
defer wg.Done()
if m.Status[k] != "OK" {
var command, url string
if m.Command != "" {
command = m.Command
} else {
command = "msg"
}
url = fmt.Sprintf("http://%s/%s", t, command)
r, ecall := toolkit.HttpCall(url, "POST",
toolkit.Jsonify(Message{Key: m.Key, Data: m.Data, Expiry: m.Expiry}), nil)
if ecall != nil {
m.setSuccessFail(k, "CALL ERROR: "+url+" ERR:"+ecall.Error())
} else if r.StatusCode != 200 {
m.setSuccessFail(k, fmt.Sprintf("CALL STATUS ERROR: %s ERR: %s", url, r.Status))
} else {
var result toolkit.Result
bs := toolkit.HttpContent(r)
edecode := toolkit.Unjson(bs, &result)
if edecode != nil {
m.setSuccessFail(k, "DECODE ERROR: "+string(bs)+" ERR:"+edecode.Error())
} else {
m.setSuccessFail(k, toolkit.IfEq(result.Status, toolkit.Status_OK, "OK", result.Message).(string))
}
}
}
}(wg, k, t)
}
wg.Wait()
//time.Sleep(1 * time.Millisecond)
//fmt.Printf("%d = %d \n", len(m.Targets), m.Success+m.Fail)
//}
}
示例14: initRoute
func (b *Broadcaster) initRoute() {
//-- add node
b.Route("/nodeadd", func(kr *knot.WebContext) interface{} {
url := kr.Query("node")
b.Subscibers = append(b.Subscibers, url)
kr.Server.Log().Info(fmt.Sprintf("Add node %s to %s", url, b.Address))
return "OK"
})
//-- add channel subscribtion
b.Route("/channelregister", func(k *knot.WebContext) interface{} {
k.Config.OutputType = knot.OutputJson
result := toolkit.NewResult()
cr := &ChannelRegister{}
k.GetPayload(cr)
b.addChannelSubcriber(cr.Channel, cr.Subscriber)
return result
})
//-- get the message (used for DstributeAsQue type)
b.Route("/getmsg", func(k *knot.WebContext) interface{} {
k.Config.OutputType = knot.OutputJson
result := toolkit.NewResult()
tm := toolkit.M{}
e := k.GetPayload(&tm)
if e != nil {
fmt.Println(e.Error())
result.SetErrorTxt(fmt.Sprintf("Broadcaster GetMsg Payload Error: %s", e.Error()))
} else {
key := tm.Get("Key", "").(string)
if key == "" {
result.SetErrorTxt("Broadcaste GetMsg Error: No key is provided")
} else {
m, exist := b.messages[key]
//fmt.Println(m.Key + " : " + m.Data.(string))
if exist == false {
result.SetErrorTxt("Message " + key + " is not exist")
} else {
result.Data = m.Data
//fmt.Printf("Sent data: %v \n", m.Data)
}
}
}
return result
})
//-- invoked to identify if a msg has been received (used for DistAsQue)
b.Route("/msgreceived", func(k *knot.WebContext) interface{} {
k.Config.OutputType = knot.OutputJson
result := toolkit.NewResult()
tm := toolkit.M{}
e := k.GetPayload(&tm)
if e != nil {
result.SetErrorTxt("Broadcaster MsgReceived Payload Error: " + e.Error())
} else {
key := tm.Get("key", "").(string)
subscriber := tm.Get("subscriber", "").(string)
mm, exist := b.messages[key]
if exist == false {
result.SetErrorTxt("Broadcaster MsgReceived Error: " + key + " is not exist")
} else {
for tIndex, t := range mm.Targets {
if t == subscriber {
mm.Status[tIndex] = "OK"
break
}
}
}
}
return result
})
//-- gracefully stop the server
b.Route("/stop", func(k *knot.WebContext) interface{} {
result := toolkit.NewResult()
defer func() {
if result.Status == toolkit.Status_OK {
k.Server.Stop()
}
}()
for _, s := range b.Subscibers {
url := "http://" + s + "/stop"
bs, e := webcall(url, "GET", nil)
if e != nil {
result.SetErrorTxt("Unable to stop " + s + ": " + e.Error())
}
sresult := toolkit.NewResult()
toolkit.Unjson(bs, sresult)
if sresult.Status == toolkit.Status_NOK {
result.SetErrorTxt("Unable to stop " + s + ": " + sresult.Message)
}
}
k.Config.OutputType = knot.OutputJson
return result
})
}
示例15: Fetch
func (c *Cursor) Fetch(m interface{}, n int, closeWhenDone bool) error {
if closeWhenDone {
defer c.Close()
}
// if !toolkit.IsPointer(m) {
// return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer")
// }
if n != 1 && reflect.ValueOf(m).Elem().Kind() != reflect.Slice {
return errorlib.Error(packageName, modCursor, "Fetch", "Model object should be pointer of slice")
}
e := c.prepIter()
if e != nil {
return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
}
datas := []toolkit.M{}
// lineCount := 0
//=============================
maxGetData := c.count
if n > 0 {
maxGetData = c.fetchRow + n
}
linecount := 0
for _, row := range c.reader.Sheet[c.sheetname].Rows {
isAppend := true
recData := toolkit.M{}
appendData := toolkit.M{}
for i, cell := range row.Cells {
if i < len(c.headerColumn) {
recData.Set(c.headerColumn[i].name, cell.Value)
if len(c.ConditionVal.Select) == 0 || c.ConditionVal.Select.Get("*", 0).(int) == 1 {
appendData.Set(c.headerColumn[i].name, cell.Value)
} else {
if c.ConditionVal.Select.Get(c.headerColumn[i].name, 0).(int) == 1 {
appendData.Set(c.headerColumn[i].name, cell.Value)
}
}
}
}
isAppend = c.ConditionVal.getCondition(recData)
if c.fetchRow < c.ConditionVal.skip || (c.fetchRow > (c.ConditionVal.skip+c.ConditionVal.limit) && c.ConditionVal.limit > 0) {
isAppend = false
}
if isAppend && len(appendData) > 0 {
linecount += 1
if linecount > c.fetchRow {
datas = append(datas, appendData)
c.fetchRow += 1
}
}
if c.fetchRow >= maxGetData {
break
}
}
e = toolkit.Unjson(toolkit.Jsonify(datas), m)
if e != nil {
return errorlib.Error(packageName, modCursor, "Fetch", e.Error())
}
return nil
}