Convert string data to struct: Inserted on Java AppEngine (Objectify), readed on Go AppEngine

huangapple go评论91阅读模式
英文:

Convert string data to struct: Inserted on Java AppEngine (Objectify), readed on Go AppEngine

问题

简介

嗨,我正在进行一个关于Go AppEngine的模块,我在读取一些包含嵌套模型的模型时遇到了问题。模型是Party,我想要的是Permissions。但是,当我从数据存储中获取Party时,Permissions结构字段的类型是字符串。

// 当我执行 fmt.Println(party.Permissions) 时,显示如下内容:
%!(EXTRA string=jjrz�5878654076715008 *��jjrzshowOnMessages 
*zcanInviteAssistants *zcanInviteOrganizers *zcanEditEvent 
*zroleName *	organizerzisAdmin *�z�4709220381360128 
*��jjrzshowOnMessages *zcanInviteAssistants *zcanInviteOrganizers 
*zcanEditEvent *zroleName *ownerzisAdmin *��

那个字符串是什么?有办法获取原始的结构体吗?

模型

Go

type Party struct {
  Name string `datastore:"name"`
  Permissions string `datastore:"permissions"`
}

type PartyPermission struct {
  isAdmin bool
  canInviteOrganizers bool
  canInviteAssistants bool
  canEditParty bool
  showOnMessages bool
  roleName string
}

Java

@Entity
public class Party implements Cloneable, Serializable {

    @Id
    private Long id;

    private String name;

    private Map<String, PartyPermission> permissions;

    // constructor ...

    // getters, setters, etc
}

public class PartyPermission implements Serializable {

    private static final long serialVersionUID = 3019266092062869643L;

    private boolean isAdmin;

    private boolean canInviteOrganizers;

    private boolean canInviteAssistants;

    private boolean canEditEvent;

    private boolean showsOnMessages;

    private String roleName;

    // constructor

    // getters, setters, etc

}

我尝试过的

我从周一开始使用Go,并且我不知道那个字符串可能是什么。我认为那个字符串可能是一个接口,我可以将该字符串转换为接口,然后转换为我真正想要的map[string]PartyPermission。所以,我尝试使用反射:

func (p *Party) GetPermissions() (map[string]PartyPermission) {
  iPermissions := reflect.ValueOf(p.Permissions).Interface()
  return iPermissions.(map[string]PartyPermission)  
}

这段代码可以编译通过,但是Go说字符串无法转换为其他类型。类似于:

interface conversion: interface is string, not map[string]packName.PartyPermission

谢谢你的时间。

英文:

The Intro

Hi there, I am doing a module on Go AppEngine and I have problems reading some models, that have nested models inside. The model is Party, and what I want is Permissions. But, when I get Party from datastore, Permissions struct field is of type string.

<!-- language: lang-golang -->

//And when I do fmt.Println(party.Permissions) show this:

%!(EXTRA string=jjrz�5878654076715008 *��jjrzshowOnMessages 
*zcanInviteAssistants *zcanInviteOrganizers *zcanEditEvent 
*zroleName *	organizerzisAdmin *�z�4709220381360128 
*��jjrzshowOnMessages *zcanInviteAssistants *zcanInviteOrganizers 
*zcanEditEvent *zroleName *ownerzisAdmin *��

What is that string? There is a way to get the original struct?

The Models

Go

<!-- language: lang-golang -->

type Party struct {
  Name string `datastore:&quot;name&quot;`
  Permissions string `datastore:&quot;permissions&quot;`
}

type PartyPermission struct {
  isAdmin bool
  canInviteOrganizers bool
  canInviteAssistants bool
  canEditParty bool
  showOnMessages bool
  roleName string
}

Java

<!-- language: lang-java -->

@Entity
public class Party implements Cloneable, Serializable {

    @Id
    private Long id;

    private String name;

    private Map&lt;String, PartyPermission&gt; permissions;

    // constructor ...

    // getters, setters, etc
}

public class PartyPermission implements Serializable {

	private static final long serialVersionUID = 3019266092062869643L;

    private boolean isAdmin;

    private boolean canInviteOrganizers;

    private boolean canInviteAssistants;

    private boolean canEditEvent;

    private boolean showsOnMessages;

    private String roleName;

    // constructor

    // getters, setters, etc

}

What I've tried

I started with go, on monday 10, and I don't recognize what could be that string. I think that maybe that string was an interface and I could convert that string to an interface, and then to what I really wish, map[string]PartyPermission. So, reflection:

<!-- language: lang-golang -->

func (p *Party) GetPermissions() (map[string]PartyPermission) {

  iPermissions := reflect.ValueOf(p.Permissions).Interface()

  return iPermissions.(map[string]PartyPermission)  
}

That compiles great, but Go said that string couldn't be converted to another thing. Something like:

interface conversion: interface is string, not map[string]packName.PartyPermission

Thanks for your time.

答案1

得分: 0

你无法直接翻译,你需要为该格式创建一个解码器。

你可以使用 JSON 或其他方式。

英文:

You can't, you have to create a decoder for that format.

You could use json or something.

huangapple
  • 本文由 发表于 2015年8月20日 05:50:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/32106231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定