使用GO语言从文件中解析JSON内容,并使用GO模板包生成.go文件。

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

Unmarshalling JSON content from a a file using GO and generate .go files using the GO template package

问题

我对读取和解析 JSON 格式的模式文件很感兴趣。我有一些在 .GO 文件中定义的 JSON 结构,我想为模式中的每种结构生成相应的 .go 文件,并使用模板包(http://golang.org/pkg/text/template/)生成这些文件中执行 CRUD 操作的代码。

以下是模式文件中的一个结构示例:

{
    "type": "collection",
    "resourceType": "schema",
    "links": {
        "self": ".../v1/schemas"
    },
    "createTypes": {},
    "actions": {},
    "data": [
        {
            "id": "schema",
            "type": "schema",
            "links": {
                "self": "/schemas/schema",
                "collection": ".../schemas"
            }
        },
        ...
    ]
}

请问有人可以帮助我使用 GO 模板包为不同的结构生成这些 CRUD 操作的代码吗?

英文:

I am interested in reading a schemas(json formatted text file) and unmarshall it as schemas (for which i have some JSON structures defined in a .GO file) and For each type of structure in the Schema, I want to generate a corresponding .go file which has the code for performing CRUD operations using the template package (http://golang.org/pkg/text/template/) to generate these files.

Example of a structure in a schema file -
{

type struct XYZ {
    Type         string                 `json:"type,omitempty"`
	ResourceType string                 `json:"resourceType,omitempty"`
	Links        map[string]string      `json:"links,omitempty"`

}

The text file has a JSON structured data which is something of this form -

{
        "type": "collection",
		"resourceType": "schema",
		"links": {
		"self": "…/v1/schemas",
		},
		"createTypes": { },
		"actions": { },
		"data": [ 86 items
		{
		"id": "schema",
		"type": "schema",
		"links": {
		"self": "/schemas/schema",
		"collection": "…/schemas",
		},
         ...

     }

}

Could somebody help me how could i possibly generate the code for these CRUD operations for different structs using the GO template package.

答案1

得分: 2

你可能会发现go generate很有用。


> 提案:go generate
>
> 提议为Go 1.4 提供新的go工具子命令。请参阅设计文档并在此线程中发表评论。
>
> http://golang.org/s/go1.4-generate
>
> -rob


> Go generate:一个提案
>
> 介绍
>
> go build命令自动化构建Go程序,但有时需要进行预处理,而go build不支持。激励示例包括:
>
> * yacc:从yacc语法(.y)文件生成.go文件
> * protobufs:从协议缓冲区定义(.proto)文件生成.pb.go文件
> * Unicode:从UnicodeData.txt生成表格
> * HTML:将.html文件嵌入到Go源代码中
> * bindata:将二进制文件(如JPEG)转换为Go源代码中的字节数组
>
>
> 还可以想象其他处理步骤:
>
> * 字符串方法:为用作枚举常量的类型生成String()字符串方法
> * 宏:根据通用包生成定制实现,例如从ints生成sort.Ints
>
> 该提案提供了一种平滑自动化这种处理的设计。

英文:

You might find go generate useful.


> proposal: go generate
>
> New go tool subcommand proposed for Go
> 1.4. Please see the design document and comment in this thread.
>
> http://golang.org/s/go1.4-generate
>
> -rob


> Go generate: A Proposal
>
> Introduction
>
> The go build command automates the construction of Go programs but
> sometimes preliminary processing is required, processing that go build
> does not support. Motivating examples include:
>
> * yacc: generating .go files from yacc grammar (.y) files
> * protobufs: generating .pb.go files from protocol buffer definition (.proto) files
> * Unicode: generating tables from UnicodeData.txt
> * HTML: embedding .html files into Go source code
> * bindata: translating binary files such as JPEGs into byte arrays in Go source
>
>
> There are other processing steps one can imagine:
>
> * string methods: generating String() string methods for types used as enumerated constants
> * macros: generating customized implementations given generalized packages, such as sort.Ints from ints
>
> This proposal offers a design for smooth automation of such
> processing.

huangapple
  • 本文由 发表于 2014年11月22日 10:42:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/27073484.html
匿名

发表评论

匿名网友

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

确定