<p>I'm trying to create a migration system for ArangoDB. I've got a Java version, but like the idea of a single deployable for my users rather than forcing the JVM on them. You can find it here <a href="https://github.com/deusdat/migrantverde">https://github.com/deusdat/migrantverde</a></p>
<p>A user can perform a series of Migrations. Each migration has an up or down action, which will modify the database as per the operation defined in the respective action. I can have a lot of operations.</p>
<p>At present I have a the following struct hierarchy</p>
<pre><code>type Migration struct {
Up Operation
Down Operation
}
type Operation struct {
Name string
Action Action
}
type Action string
const (
CREATE Action = "create"
DELETE Action = "delete"
MODIFY Action = "modify"
)
type Collection struct {
Operation
ShardKeys []string
JournalSize int
NumberOfShards int
WaitForSync bool
AllowUserKeys bool
Volatile bool
Compactable bool
}
</code></pre>
<p>The Collection struct represents idea of a database collection in ArangoDB. The Operation.action of "create" would mean create a new database collection named Operation.name.</p>
<p>When I try to set a Collection to a Migration.Up, get this error: ../src/github.com/deusdat/arangomigo/migrations_test.go:24:27: cannot use c (type Collection) as type Operation in field value.</p>
<p>What am I missing here? I know that Golang doesn't have hierarchies like Java, but I assume this is possible.</p>
<p>P.S. the goal is to read YAML files that fit this struct style. Each migration file has the Migration and the various operations. For example, I need to add a Graph struct that allows the user to define a new Graph instance or delete the graph if the operation is down.</p>
<p>Thank you.</p>
<hr/>**评论:**<br/><br/>TheMerovius: <pre><blockquote>
<p>When I try to set a Collection to a Migration.Up, get this error: ../src/github.com/deusdat/arangomigo/migrations_test.go:24:27: cannot use c (type Collection) as type Operation in field value.</p>
</blockquote>
<p>You are misunderstanding embedding, thinking it is a form of inheritance, but it's not. Inheritance provides an "is-a" relationship (<code>class Dog extends Animal</code> makes <code>Dog</code> a subtype of <code>Animal</code>, every Dog is-an Animal), but embedding provides a "has-a" relationship (<code>struct FooClient { *http.Client }</code> does <em>not</em> make <code>FooClient</code> a subtype of <code>*http.Client</code>, it just promotes the methods and fields of <code>*http.Client</code>, but they remain unrelated types. A <code>FooClient</code> has-an <code>*http.Client</code> - in this case, it uses the client it has to make HTTP requests).</p>
<p>Go also provides a way to subtype, but not via struct-embedding, but via interfaces. E.g. a <code>*bufio.Reader</code> is-an <code>io.Reader</code>, because it has a <code>Read([]byte) (int, error)</code> method and the latter is an interface with that method set.</p>
<p>Now, I don't <em>fully</em> understand what you are trying to do, but you probably want one of these:</p>
<ul>
<li>Change your Migration to have Collection fields ("every migration ups some collection or downs some collection"), instead of Operation</li>
<li>Make the Collection a field of Operation, instead of the other way around ("every operation operates on some collection")</li>
<li>Make Operation an interface (e.g. <code>interface { Execute(db *DB) error }</code>) and have Collection implement that interface ("A Migration has an Up and a Down Operation. A Collection is-an Operation, i.e. can be brought up or down")</li>
<li>Make Migration an interface and have Operation implement that interface ("A Migration is something you can do to a database. You can perform an operation on a database")</li>
</ul>
<p>Hope that helps :)</p></pre>0xjnml: <pre><p>Please turn the problem into a small, self-contained program at the Go Playground.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传