-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from numary/feature/rules
Add contracts.
- Loading branch information
Showing
18 changed files
with
756 additions
and
10 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,79 @@ | ||
package controllers | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"github.com/numary/ledger/pkg/core" | ||
"github.com/numary/ledger/pkg/ledger" | ||
"net/http" | ||
) | ||
|
||
type MappingController struct { | ||
BaseController | ||
} | ||
|
||
func NewMappingController() MappingController { | ||
return MappingController{} | ||
} | ||
|
||
// PutMapping godoc | ||
// @Summary Put mapping | ||
// @Description Update ledger mapping | ||
// @Tags mapping | ||
// @Schemes | ||
// @Param ledger path string true "ledger" | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {object} controllers.BaseResponse | ||
// @Failure 404 {object} controllers.BaseResponse | ||
// @Router /{ledger}/mapping [put] | ||
func (ctl *MappingController) PutMapping(c *gin.Context) { | ||
l, _ := c.Get("ledger") | ||
|
||
mapping := &core.Mapping{} | ||
err := c.ShouldBind(mapping) | ||
if err != nil { | ||
ctl.responseError(c, http.StatusBadRequest, err) | ||
return | ||
} | ||
|
||
err = l.(*ledger.Ledger).SaveMapping(c.Request.Context(), *mapping) | ||
if err != nil { | ||
ctl.responseError( | ||
c, | ||
http.StatusInternalServerError, | ||
err, | ||
) | ||
return | ||
} | ||
ctl.response( | ||
c, | ||
http.StatusOK, | ||
mapping, | ||
) | ||
} | ||
|
||
// GetMapping godoc | ||
// @Summary Get mapping | ||
// @Description Get ledger mapping | ||
// @Tags contracts | ||
// @Schemes | ||
// @Param ledger path string true "ledger" | ||
// @Accept json | ||
// @Produce json | ||
// @Success 200 {object} controllers.BaseResponse | ||
// @Failure 404 {object} controllers.BaseResponse | ||
// @Router /{ledger}/mapping [get] | ||
func (ctl *MappingController) GetMapping(c *gin.Context) { | ||
l, _ := c.Get("ledger") | ||
|
||
mapping, err := l.(*ledger.Ledger).LoadMapping(c.Request.Context()) | ||
if err != nil { | ||
ctl.responseError(c, http.StatusInternalServerError, err) | ||
return | ||
} | ||
ctl.response( | ||
c, | ||
http.StatusOK, | ||
mapping, | ||
) | ||
} |
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,39 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
type Contract struct { | ||
Expr Expr `json:"expr"` | ||
Account string `json:"account"` | ||
} | ||
|
||
func (c *Contract) UnmarshalJSON(data []byte) error { | ||
type AuxContract Contract | ||
type Aux struct { | ||
AuxContract | ||
Expr map[string]interface{} `json:"expr"` | ||
} | ||
aux := Aux{} | ||
err := json.Unmarshal(data, &aux) | ||
if err != nil { | ||
return err | ||
} | ||
expr, err := ParseRuleExpr(aux.Expr) | ||
if err != nil { | ||
return err | ||
} | ||
*c = Contract{ | ||
Expr: expr, | ||
Account: aux.Account, | ||
} | ||
return nil | ||
} | ||
|
||
func (c Contract) Match(addr string) bool { | ||
r := strings.ReplaceAll(c.Account, "*", ".*") | ||
return regexp.MustCompile(r).Match([]byte(addr)) | ||
} |
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,14 @@ | ||
package core | ||
|
||
import ( | ||
"encoding/json" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestContract_UnmarshalJSON(t *testing.T) { | ||
contract := &Contract{} | ||
data := `{"id": "foo", "account": "order:*", "expr": { "$gte": ["$balance", 0] }}` | ||
err := json.Unmarshal([]byte(data), contract) | ||
assert.NoError(t, err) | ||
} |
Oops, something went wrong.