Complex structure

agolangf · · 505 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p>I&#39;m trying to create a migration system for ArangoDB. I&#39;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 = &#34;create&#34; DELETE Action = &#34;delete&#34; MODIFY Action = &#34;modify&#34; ) 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 &#34;create&#34; 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&#39;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&#39;s not. Inheritance provides an &#34;is-a&#34; 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 &#34;has-a&#34; 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&#39;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 (&#34;every migration ups some collection or downs some collection&#34;), instead of Operation</li> <li>Make the Collection a field of Operation, instead of the other way around (&#34;every operation operates on some collection&#34;)</li> <li>Make Operation an interface (e.g. <code>interface { Execute(db *DB) error }</code>) and have Collection implement that interface (&#34;A Migration has an Up and a Down Operation. A Collection is-an Operation, i.e. can be brought up or down&#34;)</li> <li>Make Migration an interface and have Operation implement that interface (&#34;A Migration is something you can do to a database. You can perform an operation on a database&#34;)</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

505 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传