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

codecgen ignores struct tags for struct field renaming #60

Closed
starJammer opened this issue Apr 20, 2015 · 8 comments
Closed

codecgen ignores struct tags for struct field renaming #60

starJammer opened this issue Apr 20, 2015 · 8 comments

Comments

@starJammer
Copy link

Hi, I've been looking at codec recently and was testing it out but was getting some behavior differences between it and encoding/json. Here is the output I'm getting using the same structs.

Encodings from codec generated selfer code....
{"Key":"thing"}
{"Key":"whynot","Field1":"another"}
Encodings from codec reflection based encoding....
{"_key":"thing"}
{"_id":"another","_key":"whynot"}
Encodings from encoding/json....
{"_key":"thing"}
{"_key":"whynot","_id":"another"}

Below is my example code and directory structure

root
|----main.go
|----model
|--------model.go

The main.go file

package main

import (
    "fmt"
    "github.com/ugorji/go/codec" 
    "encoding/json"
    "./model"
)

type Test struct {
    Key string `json:"_key,omitempty"`
}

type Next struct {
    Test
    Field1 string `json:"_id,omitempty"`
    Field2 string `json:",omitempty"`
}


func main() {
    var h codec.Handle = new(codec.JsonHandle)
    var b []byte

    d := &model.Test{ Key : "thing" }
    u := &model.Next{ Field1: "another" }

    f := &Test{ Key : "thing" }
    t := &Next{ Field1: "another" }

    u.Key = "whynot"
    t.Key = "whynot" //Edited to add this line since it was missing

    fmt.Println( "Encodings from codec generated selfer code...." )
    b, _ = encodeCodec( d, h )
    fmt.Println( string(b) )
    b, _ = encodeCodec( u, h )
    fmt.Println( string(b) )

    fmt.Println( "Encodings from codec reflection based encoding...." )
    b, _ = encodeCodec( f, h )
    fmt.Println( string(b) )
    b, _ = encodeCodec( t, h )
    fmt.Println( string(b) )

    fmt.Println( "Encodings from encoding/json...." )
    b, _ = encodeJson( d )
    fmt.Println( string(b) )
    b, _ = encodeJson( u )
    fmt.Println( string(b) )

}

func encodeCodec( v interface{}, h codec.Handle ) ( []byte, error ) {
    var b []byte
    enc := codec.NewEncoderBytes( &b, h )
    err := enc.Encode( v )
    if err != nil {
        fmt.Println( err )
        return nil, err
    }
    return b, nil
}

func encodeJson( v interface{} ) ( []byte, error ){
    b, err := json.Marshal( v )

    if err != nil {
        fmt.Println( err )
        return nil, err
    }
    return b, nil
}

Here is the model.go file

package model
//go:generate codecgen -o codec_gen.go model.go

type Test struct {
    Key string `json:"_key,omitempty"`
}

type Next struct {
    Test
    Field1 string `json:"_id,omitempty"`
    Field2 string `json:",omitempty"`
}
@ugorji
Copy link
Owner

ugorji commented Apr 20, 2015

The only problem is with the codec-generated one from what i see.

i.e. codec generated one doesn't take the struct tags into account.

Is that correct?

If so, can you ensure that you have the latest version of codecgen? Do a 'go get -u github.com/ugorji/go/codec/codecgen github.com/ugorji/go/codec'

@ugorji
Copy link
Owner

ugorji commented Apr 20, 2015

i.e. if you do a

t.Key="whynot"

Then you will see that both runtime-introspection (reflection) modes work the same. The only discrepancy is in the generated code, as it looks like it does honor the struct tags. I believe this was fixed a while back, which is why I want you to try and re-update, to see if the issue currently exists, before i look deeper.

Thanks much.

@starJammer
Copy link
Author

Hey Ugorij,
Thanks for responding. I downloaded the code this morning with the -u option so I doubt that is the issue. Today was also the first time I ran go get. I'll update the code again anyway using the command you gave me just to be safe.

Yes, you're right. Only 1 issue. I didn't set the Key field of the second struct I was testing.

With the codec generated code:

  1. Yes, the struct tags aren't taken into account. Specifically, they are using the struct field names instead

@starJammer
Copy link
Author

I ran the following commands

  1. rm codecgen in the bin directory where it was installed. In my $GO_BIN
  2. go get -u github.com/ugorji/go/codec/codecgen github.com/ugorji/go/codec
  3. codecgen -o codec_gen.go model.go to regenerate the generated code.
  4. go build main.go in root to build
  5. ./main to run the program.

I still received incorrect output for the generated code. The reflection based code came out correct though. I updated my original post to reflect this.

@ugorji ugorji changed the title JsonHandle inconsistent with encoding/json encodings code generation ignores struct tags for struct field renaming Apr 21, 2015
@ugorji ugorji changed the title code generation ignores struct tags for struct field renaming codecgen ignores struct tags for struct field renaming Apr 21, 2015
@ugorji ugorji closed this as completed in f1401fe Apr 22, 2015
@ugorji
Copy link
Owner

ugorji commented Apr 22, 2015

@starJammer Please try now with updates and let me kow that it works.

Thanks.

@starJammer
Copy link
Author

I think things are worse now. The codecgen command is spitting out an error and two very small files are being generated when I use the -x option.

Command I ran: codecgen -x=true -o=codec_gen.go model.go

codecgen error: Error running go run codecgen-main-1429668731680257800.generated.go. Error: exit status 2. stdout/err: # command-line-arguments
./codecgen-main-1429668731680257800.generated.go:6: undefined: model.CodecGenTempWrite1429668731680257800

The codecgen-main-1429668731680257800.generated.go file

//+build ignore

package main
import "bitbucket.org/starJammer/tester/model"
func main() {
    model.CodecGenTempWrite1429668731680257800()
}

The codecgen-pkg-1429668731680257800.generated.go file

//+build codecgen

package model

import (
    codec1978 "github.com/ugorji/go/codec"

    "os"
    "reflect"
    "bytes"
    "go/format"
)



func CodecGenTempWrite1429668731680257800() {
    fout, err := os.Create("codec_gen.go")
    if err != nil {
        panic(err)
    }
    defer fout.Close()
    var out bytes.Buffer

    var typs []reflect.Type 

    var t0 Test
    typs = append(typs, reflect.TypeOf(t0))

    var t1 Next
    typs = append(typs, reflect.TypeOf(t1))

    codec1978.Gen(&out, "", "model", false, typs...)
    bout, err := format.Source(out.Bytes())
    if err != nil {
        fout.Write(out.Bytes())
        panic(err)
    }
    fout.Write(bout)
}

ugorji added a commit that referenced this issue Apr 22, 2015
We already ensure that we generate a transient method name to call.
No need to use build tags to restrict it, as that requires each
codecgen user to pass the -rt codecgen flag when calling codecgen.

Resolves #60
@ugorji
Copy link
Owner

ugorji commented Apr 22, 2015

The codecgen-pkg-1429668731680257800.generated.go should not have had the //+build codecgen,
as that would require that each call to codecgen passes the '-rt codecgen' option.

This should be resolved now.

Please retry.

Thanks.

@starJammer
Copy link
Author

The resolves it. Thank you for the amazing support.

Encodings from codec generated selfer code....
{"_key":"thing"}
{"_key":"whynot","_id":"another"}
Encodings from codec reflection based encoding....
{"_key":"thing"}
{"_id":"another","_key":"whynot"}
Encodings from encoding/json....
{"_key":"thing"}
{"_key":"whynot","_id":"another"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants