Skip to content

Commit

Permalink
Merge branch 'master' into feat/1812
Browse files Browse the repository at this point in the history
  • Loading branch information
omarsy committed May 21, 2024
2 parents 7cfe142 + 7d939a0 commit 3435ac4
Show file tree
Hide file tree
Showing 32 changed files with 700 additions and 31 deletions.
20 changes: 18 additions & 2 deletions examples/gno.land/p/demo/blog/blog.gno
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (b *Blog) prepareAndSetPost(post *Post) error {
post.Blog = b
post.UpdatedAt = time.Now()

trimmedTitleKey := strings.Replace(post.Title, " ", "", -1)
pubDateKey := post.CreatedAt.Format(time.RFC3339)
trimmedTitleKey := getTitleKey(post.Title)
pubDateKey := getPublishedKey(post.CreatedAt)

// Cannot have two posts with same title key
if _, found := b.PostsAlphabetical.Get(trimmedTitleKey); found {
Expand All @@ -195,6 +195,22 @@ func (b *Blog) prepareAndSetPost(post *Post) error {
return nil
}

func (b *Blog) RemovePost(slug string) {
p, exists := b.Posts.Get(slug)
if !exists {
panic("post with specified slug doesn't exist")
}

post := p.(*Post)

titleKey := getTitleKey(post.Title)
publishedKey := getPublishedKey(post.CreatedAt)

_, _ = b.Posts.Remove(slug)
_, _ = b.PostsAlphabetical.Remove(titleKey)
_, _ = b.PostsPublished.Remove(publishedKey)
}

func (b *Blog) GetPost(slug string) *Post {
post, found := b.Posts.Get(slug)
if !found {
Expand Down
13 changes: 12 additions & 1 deletion examples/gno.land/p/demo/blog/util.gno
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
package blog

import "strings"
import (
"strings"
"time"
)

func breadcrumb(parts []string) string {
return "# " + strings.Join(parts, " / ") + "\n\n"
}

func getTitleKey(title string) string {
return strings.Replace(title, " ", "", -1)
}

func getPublishedKey(t time.Time) string {
return t.Format(time.RFC3339)
}
6 changes: 6 additions & 0 deletions examples/gno.land/r/gnoland/blog/admin.gno
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func ModEditPost(slug, title, body, publicationDate, authors, tags string) {
checkErr(err)
}

func ModRemovePost(slug string) {
assertIsModerator()

b.RemovePost(slug)
}

func ModAddCommenter(addr std.Address) {
assertIsModerator()
commenterList.Set(addr.String(), true)
Expand Down
29 changes: 29 additions & 0 deletions examples/gno.land/r/gnoland/blog/gnoblog_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,35 @@ Published by g1u7y667z64x2h7vc6fmpcprgey4ck233jaww9zq to Gnoland's Blog
assertMDEquals(t, got, expected)
}

{ // Test remove functionality
title := "example title"
slug := "testSlug1"
ModAddPost(slug, title, "body1", "2022-05-25T13:17:22Z", "moul", "tag1,tag2")

got := Render("")

if !strings.Contains(got, title) {
t.Errorf("post was not added properly")
}

postRender := Render("p/" + slug)

if !strings.Contains(postRender, title) {
t.Errorf("post not rendered properly")
}

ModRemovePost(slug)
got = Render("")

if strings.Contains(got, title) {
t.Errorf("post was not removed")
}

postRender = Render("p/" + slug)

assertMDEquals(t, postRender, "404")
}

// TODO: pagination.
// TODO: ?format=...

Expand Down
11 changes: 8 additions & 3 deletions gno.land/pkg/gnoweb/static/css/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,10 @@ html[data-theme="light"] {
--quote-background: #ddd;
--quote-2-background: #aaa4;
--code-background: #d7d9db;
--header-background: #6b6d73;
--header-background: #373737;
--header-forground: #ffffff;
--logo-hat: #ffffff;
--logo-beard: #808080;

--realm-help-background-color: #d7d9db9e;
--realm-help-odd-background-color: #d7d9db45;
Expand Down Expand Up @@ -85,8 +87,10 @@ html[data-theme="dark"] {
--quote-background: #404040;
--quote-2-background: #555555;
--code-background: #606060;
--header-background: #6b6d73;
--header-background: #373737;
--header-forground: #ffffff;
--logo-hat: #ffffff;
--logo-beard: #808080;

--realm-help-background-color: #45454545;
--realm-help-odd-background-color: #4545459e;
Expand All @@ -111,7 +115,8 @@ html[data-theme="dark"] {
}

.logo-wording path {fill: var(--header-forground, #ffffff); }
.logo-beard { fill: var(--header-forground, #ffffff); }
.logo-beard { fill: var(--logo-beard, #808080); }
.logo-hat {fill: var(--logo-hat, #ffffff); }

#theme-toggle {
display: inline-block;
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gno.land/pkg/gnoweb/static/img/favicon-16x16.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added gno.land/pkg/gnoweb/static/img/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified gno.land/pkg/gnoweb/static/img/favicon.ico
Binary file not shown.
Binary file added gno.land/pkg/gnoweb/static/img/og-gnoland.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
29 changes: 29 additions & 0 deletions gno.land/pkg/gnoweb/static/img/safari-pinned-tab.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 11 additions & 1 deletion gno.land/pkg/gnoweb/views/funcs.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="stylesheet" href="/static/css/normalize.css" />
<link rel="stylesheet" href="/static/css/app.css" />
<link rel="apple-touch-icon" sizes="180x180" href="/static/img/apple-touch-icon.png" />
<link rel="icon" type="image/png" sizes="32x32" href="/static/img/favicon-32x32.png" />
<link rel="icon" type="image/png" sizes="16x16" href="/static/img/favicon-16x16.png" />
<link rel="mask-icon" href="/static/img/safari-pinned-tab.svg" color="#6f6f6f" />
<meta name="theme-color" content="#ffffff" />
<meta property="og:title" content="Gno.land - {{.Data.RealmName}}" />
<meta property="og:url" content="https://gno.land" />
<meta property="og:image" content="https://gno.land/static/img/og-gnoland.png" />
<meta property="og:site_name" content="gno.land" />
<meta property="og:description" content="{{ .Data.Description }}" />
<noscript>
<style type="text/css">
#source { display: block; }
Expand Down Expand Up @@ -121,7 +131,7 @@
<a class="logo" href="/">
<svg class="logo_img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1000 238">
<path d="M136.02,160.26c-1.77-6.5-5.55-12.22-10.68-16.59-1.98-1.69-4.14-3.29-6.5-4.78-2-1.27-4.52.6-3.94,2.9l1.42,5.62c1.8,7.14-5.86,12.93-12.24,9.26l-17.15-9.86c-11.27-6.48-25.13-6.48-36.4,0l-17.15,9.86c-6.38,3.67-14.04-2.13-12.24-9.26l1.46-5.78c.58-2.29-1.93-4.16-3.93-2.9-2.62,1.64-5,3.42-7.16,5.29-5.05,4.38-8.56,10.26-10.2,16.74l-.06.24c-4.17,16.56,2.31,33.97,16.29,43.78l43.3,30.37c4.74,3.32,11.05,3.32,15.79,0l43.3-30.37c14.19-9.95,20.65-27.74,16.09-44.51Z" class="logo-beard" />
<path d="M134.22,123.74c-3.78-31.58-19.27-63.22-34.25-87.46l22.26-22.26c5.04-5.04,1.47-13.66-5.66-13.66h-47.94c-3.7,0-7.41,1.63-9.91,4.88C41.84,27.21,8.79,75.55,3.02,123.74c-.52,4.39,4.63,7.08,7.93,4.14,11.52-10.26,29.49-17.6,57.67-17.6s46.14,7.35,57.67,17.6c3.3,2.94,8.45.24,7.93-4.14Z" style="fill: #ee445d" />
<path d="M134.22,123.74c-3.78-31.58-19.27-63.22-34.25-87.46l22.26-22.26c5.04-5.04,1.47-13.66-5.66-13.66h-47.94c-3.7,0-7.41,1.63-9.91,4.88C41.84,27.21,8.79,75.55,3.02,123.74c-.52,4.39,4.63,7.08,7.93,4.14,11.52-10.26,29.49-17.6,57.67-17.6s46.14,7.35,57.67,17.6c3.3,2.94,8.45.24,7.93-4.14Z" class="logo-hat" />
<g class="logo-wording">
<path d="M190.79,202.39l13.67-21.82c9.35,10.07,21.82,14.14,36.2,14.14s32.13-6.23,32.13-29.73v-11.27c-9.11,11.51-21.58,17.98-35.96,17.98-28.77,0-51.06-20.14-51.06-58.74s21.82-58.98,51.06-58.98c13.91,0,26.61,5.75,35.96,17.74v-14.86h30.45v108.12c0,43.87-34.04,54.66-62.57,54.66-19.66,0-35.24-4.55-49.87-17.26ZM272.78,131.66v-37.64c-5.28-7.43-16.3-12.95-26.13-12.95-17.5,0-29.49,11.99-29.49,31.89s11.99,31.89,29.49,31.89c9.83,0,20.86-5.75,26.13-13.19Z" />
<path d="M397.33,172.66v-70c0-16.06-8.39-21.58-21.34-21.58-11.99,0-21.1,6.71-26.37,13.43v78.16h-30.45V56.86h30.45v14.86c7.43-8.63,21.82-17.74,40.52-17.74,25.65,0,37.88,14.38,37.88,36.92v81.75h-30.69Z" />
Expand Down
6 changes: 4 additions & 2 deletions gnovm/pkg/gnolang/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -2164,8 +2164,10 @@ func (m *Machine) Stacktrace() string {

for i := len(ex.Stack.Execs) - 1; i >= 0; i-- {
exec := ex.Stack.Execs[i]
builder.WriteString(fmt.Sprintf(" %v\n", exec.Stmt))
builder.WriteString(fmt.Sprintf(" %s/%s:%d\n", exec.Fun.PkgPath, exec.Fun.FileName, exec.Stmt.GetLine()))
if exec.Stmt != nil {
builder.WriteString(fmt.Sprintf(" %v\n", exec.Stmt))
builder.WriteString(fmt.Sprintf(" %s/%s:%d\n", exec.Func.PkgPath, exec.Func.FileName, exec.Stmt.GetLine()))

Check warning on line 2169 in gnovm/pkg/gnolang/machine.go

View check run for this annotation

Codecov / codecov/patch

gnovm/pkg/gnolang/machine.go#L2165-L2169

Added lines #L2165 - L2169 were not covered by tests
}
}
}

Expand Down
107 changes: 89 additions & 18 deletions gnovm/pkg/gnolang/op_expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,16 +204,41 @@ func (m *Machine) doOpRef() {
func (m *Machine) doOpTypeAssert1() {
m.PopExpr()
// pop type
t := m.PopValue().GetType()
t := m.PopValue().GetType() // type being asserted

// peek x for re-use
xv := m.PeekValue(1)
xt := xv.T
xv := m.PeekValue(1) // value result / value to assert
xt := xv.T // underlying value's type

// xt may be nil, but we need to wait to return because the value of xt that is set
// will depend on whether we are trying to assert to an interface or concrete type.
// xt can be nil in the case where recover can't find a panic to recover from and
// returns a bare TypedValue{}.

if t.Kind() == InterfaceKind { // is interface assert
if xt == nil || xv.IsNilInterface() {
// TODO: default panic type?
ex := fmt.Sprintf("interface conversion: interface is nil, not %s", t.String())
m.Panic(typedString(ex))
return
}

if it, ok := baseOf(t).(*InterfaceType); ok {
// An interface type assertion on a value that doesn't have a concrete base
// type should always fail.
if _, ok := baseOf(xt).(*InterfaceType); ok {
// TODO: default panic type?
ex := fmt.Sprintf(
"non-concrete %s doesn't implement %s",
xt.String(),
it.String())
m.Panic(typedString(ex))
return
}

// t is Gno interface.
// assert that x implements type.
impl := false
var impl bool
impl = it.IsImplementedBy(xt)
if !impl {
// TODO: default panic type?
Expand All @@ -230,16 +255,22 @@ func (m *Machine) doOpTypeAssert1() {
} else if nt, ok := baseOf(t).(*NativeType); ok {
// t is Go interface.
// assert that x implements type.
impl := false
errPrefix := "non-concrete "
var impl bool
if nxt, ok := xt.(*NativeType); ok {
impl = nxt.Type.Implements(nt.Type)
} else {
impl = false
// If the underlying native type is reflect.Interface kind, then this has no
// concrete value and should fail.
if nxt.Type.Kind() != reflect.Interface {
impl = nxt.Type.Implements(nt.Type)
errPrefix = ""
}
}

if !impl {
// TODO: default panic type?
ex := fmt.Sprintf(
"%s doesn't implement %s",
"%s%s doesn't implement %s",
errPrefix,
xt.String(),
nt.String())
m.Panic(typedString(ex))
Expand All @@ -251,6 +282,12 @@ func (m *Machine) doOpTypeAssert1() {
panic("should not happen")
}
} else { // is concrete assert
if xt == nil {
ex := fmt.Sprintf("nil is not of type %s", t.String())
m.Panic(typedString(ex))
return
}

tid := t.TypeID()
xtid := xt.TypeID()
// assert that x is of type.
Expand All @@ -273,17 +310,37 @@ func (m *Machine) doOpTypeAssert1() {
func (m *Machine) doOpTypeAssert2() {
m.PopExpr()
// peek type for re-use
tv := m.PeekValue(1)
t := tv.GetType()
tv := m.PeekValue(1) // boolean result
t := tv.GetType() // type being asserted

// peek x for re-use
xv := m.PeekValue(2)
xt := xv.T
xv := m.PeekValue(2) // value result / value to assert
xt := xv.T // underlying value's type

// xt may be nil, but we need to wait to return because the value of xt that is set
// will depend on whether we are trying to assert to an interface or concrete type.
// xt can be nil in the case where recover can't find a panic to recover from and
// returns a bare TypedValue{}.

if t.Kind() == InterfaceKind { // is interface assert
if xt == nil {
*xv = TypedValue{}
*tv = untypedBool(false)
return
}

if it, ok := baseOf(t).(*InterfaceType); ok {
// An interface type assertion on a value that doesn't have a concrete base
// type should always fail.
if _, ok := baseOf(xt).(*InterfaceType); ok {
*xv = TypedValue{}
*tv = untypedBool(false)
return
}

// t is Gno interface.
// assert that x implements type.
impl := false
var impl bool
impl = it.IsImplementedBy(xt)
if impl {
// *xv = *xv
Expand All @@ -295,14 +352,18 @@ func (m *Machine) doOpTypeAssert2() {
*tv = untypedBool(false)
}
} else if nt, ok := baseOf(t).(*NativeType); ok {
// If the value being asserted on is nil, it can't implement an interface.
// t is Go interface.
// assert that x implements type.
impl := false
var impl bool
if nxt, ok := xt.(*NativeType); ok {
impl = nxt.Type.Implements(nt.Type)
} else {
impl = false
// If the underlying native type is reflect.Interface kind, then this has no
// concrete value and should fail.
if nxt.Type.Kind() != reflect.Interface {
impl = nxt.Type.Implements(nt.Type)
}
}

if impl {
// *xv = *xv
*tv = untypedBool(true)
Expand All @@ -314,10 +375,20 @@ func (m *Machine) doOpTypeAssert2() {
panic("should not happen")
}
} else { // is concrete assert
if xt == nil {
*xv = TypedValue{
T: t,
V: defaultValue(m.Alloc, t),
}
*tv = untypedBool(false)
return
}

tid := t.TypeID()
xtid := xt.TypeID()
// assert that x is of type.
same := tid == xtid

if same {
// *xv = *xv
*tv = untypedBool(true)
Expand Down
Loading

0 comments on commit 3435ac4

Please sign in to comment.