Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generic declaration with multiple type parameters #1460

Closed
mvertes opened this issue Sep 21, 2022 · 0 comments · Fixed by #1461
Closed

generic declaration with multiple type parameters #1460

mvertes opened this issue Sep 21, 2022 · 0 comments · Fixed by #1461
Labels
area/core bug Something isn't working

Comments

@mvertes
Copy link
Member

mvertes commented Sep 21, 2022

The following program sample.go triggers an unexpected result

package main

import (
    "bytes"
    "encoding/json"
    "errors"
    "reflect"
)   

func unmarshalJSON[T any](b []byte, x *[]T) error {
    if *x != nil {
        return errors.New("already initialized")
    }   
    if len(b) == 0 {
        return nil 
    }   
    return json.Unmarshal(b, x)
}   

type StructView[T any] interface {
    Valid() bool
    AsStruct() T
}   

type SliceView[T ViewCloner[T, V], V StructView[T]] struct {
    ж []T
}   

type ViewCloner[T any, V StructView[T]] interface {
    View() V
    Clone() T
}   

func SliceOfViews[T ViewCloner[T, V], V StructView[T]](x []T) SliceView[T, V] {
    return SliceView[T, V]{x}
}   

func (v SliceView[T, V]) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) }

func (v *SliceView[T, V]) UnmarshalJSON(b []byte) error { return unmarshalJSON(b, &v.ж) }

type Slice[T any] struct {
    ж []T
}   

func (v Slice[T]) MarshalJSON() ([]byte, error) { return json.Marshal(v.ж) }

func (v *Slice[T]) UnmarshalJSON(b []byte) error { return unmarshalJSON(b, &v.ж) }

func SliceOf[T any](x []T) Slice[T] {
    return Slice[T]{x}
}   

type viewStruct struct {
    Int        int
    Strings    Slice[string]
    StringsPtr *Slice[string] `json:",omitempty"`
}   

func main() {
    ss := SliceOf([]string{"bar"})
    in := viewStruct{
        Int:        1234,
        Strings:    ss,
        StringsPtr: &ss,
    }   
    
    var buf bytes.Buffer
    encoder := json.NewEncoder(&buf)
    encoder.SetIndent("", "")
    err1 := encoder.Encode(&in)
    b := buf.Bytes()
    var got viewStruct
    err2 := json.Unmarshal(b, &got)
    println(err1 == nil, err2 == nil, reflect.DeepEqual(got, in))
}

Expected result

$ go run ./sample.go
true true true

Got

$ yaegi ./sample.go
run: ./sample.go:34:21: type definition not implemented: indexListExpr

Yaegi Version

v0.14.2

Additional Notes

No response

@mvertes mvertes added bug Something isn't working area/core labels Sep 21, 2022
mvertes added a commit to mvertes/yaegi that referenced this issue Sep 21, 2022
Those declarations involve the indexListExpr AST token, which
was not handled in type.go. The same processing as for a single
type parameter is applied.

Fixes traefik#1460.
traefiker pushed a commit that referenced this issue Sep 22, 2022
Those declarations involve the indexListExpr AST token, which was not handled in type.go. The same processing as for a single type parameter is applied.

Fixes #1460.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/core bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant