Interface{} constrains on specific types

57 views
Skip to first unread message

Mihai B

unread,
Jun 24, 2016, 6:10:44 PM6/24/16
to golang-nuts
 I'm developing a json schema generator (json schema from Go ast and the other way around). There is a keyword "oneOf" which requires  exactly one schema to be valid in this keyword's value. In Go I translate it by using an empty interface{}. The issue is that when I convert the interface{} to json schema I can only say that the object can be of any type even if I know that it can hold only a small subset.
 Therefore I'm wondering if placing some constrains on the types that could be implemented by the interface would be a good idea. For example instead of `type interface{}` which implements any type we could define the types it can implement (e.g.  type  X interface{T1, T2, T3} ). This way we don't have a totally black box so it improves the documentation/semantics and we avoid specific bugs using static analysis. Currently the practice seems to be documenting the types in pure comments[0] which cannot be analysed statically. Another option that I'm considering now is to use the empty interface but with specific tags [1] and use an external tool. This might have been proposed before but I can't find it on the mailing list. What do you think?

[1]  
type Object struct {
        Name string      // declared name
        Decl interface{} `interface:"Field,FuncDecl,LabeledStmt,external.Scope" //
   
}

Thomas Bushnell, BSG

unread,
Jun 24, 2016, 6:57:19 PM6/24/16
to Mihai B, golang-nuts
The trick is to do this:

Decl special_interface

and then special_interface requires an unexported interface which you implement in the specific (new) types that you can store in the thing.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages