Skip to content
This repository has been archived by the owner on Apr 26, 2020. It is now read-only.

Commit

Permalink
feat: parse intall message
Browse files Browse the repository at this point in the history
Change-Id: Ied4885b9a587c2cf23d3054f980d29e2b3505c20
  • Loading branch information
Iceyer committed Dec 7, 2018
1 parent dada165 commit ebcfe90
Show file tree
Hide file tree
Showing 17 changed files with 4,823 additions and 1 deletion.
Binary file removed src/daemon/deepin-appstore-daemon
Binary file not shown.
1 change: 1 addition & 0 deletions src/daemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
}

m := NewMetadata()
m.debBackend = b
err = service.Export(dbusMetadataPath, m)
if err != nil {
logger.Fatal(err)
Expand Down
48 changes: 48 additions & 0 deletions src/daemon/message.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package main

import (
"strings"

"github.com/mitchellh/mapstructure"
)

/*
{
"type": "store",
"playload": {
"action": "install",
"apps": [{
"name": "gedit",
"packageURI": "dpk://deb/gedit"
}, {
"name": "deepin-music",
"packageURI": "dpk://deb/deepin-music"
}]
}
}
*/

type msgInstall struct {
Action string `json:"action"`
Apps []struct {
Name string `json:"name"`
PackageURI string `json:"packageURI"`
} `json:"apps"`
}

func (m *Metadata) handleInstall(playload map[string]interface{}) error {
var msg msgInstall
err := mapstructure.Decode(playload, &msg)
if nil != err {
logger.Errorf("decode message %v failed: %v", playload, err)
return err
}

for _, app := range msg.Apps {
packageName := strings.Replace(app.PackageURI, "dpk://deb/", "", -1)
logger.Infof("install %v %v", app.Name, packageName)
m.debBackend.Install(app.Name, packageName)
}

return nil
}
18 changes: 17 additions & 1 deletion src/daemon/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type Metadata struct {
cfg *ini.File
userCfg *ini.File

debBackend *Backend

apps map[string]*AppBody

methods *struct {
Expand Down Expand Up @@ -170,6 +172,20 @@ func (m *Metadata) OpenApp(appName string) *dbus.Error {

// OnMessage handle push message
func (m *Metadata) OnMessage(playload map[string]interface{}) *dbus.Error {
logger.Info(playload)
action, ok := playload["action"]
if !ok {
logger.Errorf("unknown message %v", playload)
}

var err error
switch action {
case "install":
err = m.handleInstall(playload)
}

if nil != err {
logger.Errorf("process message failed: %v", playload)
}

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
language: go

go:
- "1.11.x"
- tip

script:
- go test
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
## 1.1.2

* Fix error when decode hook decodes interface implementation into interface
type. [GH-140]

## 1.1.1

* Fix panic that can happen in `decodePtr`

## 1.1.0

* Added `StringToIPHookFunc` to convert `string` to `net.IP` and `net.IPNet` [GH-133]
* Support struct to struct decoding [GH-137]
* If source map value is nil, then destination map value is nil (instead of empty)
* If source slice value is nil, then destination slice value is nil (instead of empty)
* If source pointer is nil, then destination pointer is set to nil (instead of
allocated zero value of type)

## 1.0.0

* Initial tagged stable release.
21 changes: 21 additions & 0 deletions src/daemon/vendor/src/github.com/mitchellh/mapstructure/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2013 Mitchell Hashimoto

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
46 changes: 46 additions & 0 deletions src/daemon/vendor/src/github.com/mitchellh/mapstructure/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# mapstructure [![Godoc](https://godoc.org/github.com/mitchellh/mapstructure?status.svg)](https://godoc.org/github.com/mitchellh/mapstructure)

mapstructure is a Go library for decoding generic map values to structures
and vice versa, while providing helpful error handling.

This library is most useful when decoding values from some data stream (JSON,
Gob, etc.) where you don't _quite_ know the structure of the underlying data
until you read a part of it. You can therefore read a `map[string]interface{}`
and use this library to decode it into the proper underlying native Go
structure.

## Installation

Standard `go get`:

```
$ go get github.com/mitchellh/mapstructure
```

## Usage & Example

For usage and examples see the [Godoc](http://godoc.org/github.com/mitchellh/mapstructure).

The `Decode` function has examples associated with it there.

## But Why?!

Go offers fantastic standard libraries for decoding formats such as JSON.
The standard method is to have a struct pre-created, and populate that struct
from the bytes of the encoded format. This is great, but the problem is if
you have configuration or an encoding that changes slightly depending on
specific fields. For example, consider this JSON:

```json
{
"type": "person",
"name": "Mitchell"
}
```

Perhaps we can't populate a specific structure without first reading
the "type" field from the JSON. We could always do two passes over the
decoding of the JSON (reading the "type" first, and the rest later).
However, it is much simpler to just decode this into a `map[string]interface{}`
structure, read the "type" key, then use something like this library
to decode it into the proper structure.
Loading

0 comments on commit ebcfe90

Please sign in to comment.