func (s *Schema) Resolve(r *Schema, rs ResolvedSet) *Schema {
	if r == nil {
		r = s
	}
	for {
		if s.Ref != nil {
			s = s.Ref.Resolve(r)
		} else if len(s.OneOf) > 0 {
			s = s.OneOf[0].Ref.Resolve(r)
		} else if len(s.AnyOf) > 0 {
			s = s.AnyOf[0].Ref.Resolve(r)
		} else {
			break
		}
	}
	if rs.Has(s) {
		return s
	}
	rs.Insert(s)
	for n, d := range s.Definitions {
		s.Definitions[n] = d.Resolve(r, rs)
	}
	for n, p := range s.Properties {
		s.Properties[n] = p.Resolve(r, rs)
	}
	for n, p := range s.PatternProperties {
		s.PatternProperties[n] = p.Resolve(r, rs)
	}
	if s.Items != nil {
		s.Items = s.Items.Resolve(r, rs)
	}
	for _, l := range s.Links {
		l.Resolve(r, rs)
	}
	return s
}
Resolve resolves reference inside the schema.
package schematic
import (
	"bytes"
	"encoding/json"
	"fmt"
	"go/format"
	"os"
	"strings"
	"text/template"
	bundle "github.com/interagent/schematic/templates"
)
var templates *template.Template
func init() {
	templates = template.New("package.tmpl").Funcs(helpers)
	templates = template.Must(bundle.Parse(templates))
}
type ResolvedSet map[interface{}]bool
func (rs ResolvedSet) Insert(o interface{}) {
	rs[o] = true
}
func (rs ResolvedSet) Has(o interface{}) bool {
	return rs[o]
}
func (s *Schema) Generate() ([]byte, error) {
	var buf bytes.Buffer
	s = s.Resolve(nil, ResolvedSet{})
	name := strings.ToLower(strings.Split(s.Title, " ")[0])
	templates.ExecuteTemplate(&buf, "package.tmpl", name)
	templates.ExecuteTemplate(&buf, "imports.tmpl", []string{
		"encoding/json", "fmt", "io", "reflect", "net/http", "runtime",
		"time", "bytes", "context", "strings",
		"github.com/google/go-querystring/query",
	})
	templates.ExecuteTemplate(&buf, "service.tmpl", struct {
		Name    string
		URL     string
		Version string
	}{
		Name:    name,
		URL:     s.URL(),
		Version: s.Version,
	})
	for _, name := range sortedKeys(s.Properties) {
		schema := s.Properties[name]
		if schema.Links == nil && schema.Properties == nil {
			continue
		}
		context := struct {
			Name       string
			Definition *Schema
		}{
			Name:       name,
			Definition: schema,
		}
		if !context.Definition.AreTitleLinksUnique() {
			return nil, fmt.Errorf("duplicate titles detected for %s", context.Name)
		}
		templates.ExecuteTemplate(&buf, "struct.tmpl", context)
		templates.ExecuteTemplate(&buf, "funcs.tmpl", context)
	}
	bytes := newlines.ReplaceAll(buf.Bytes(), []byte(""))
	clean, err := format.Source(bytes)
	if err != nil {
		return buf.Bytes(), err
	}
	return clean, nil
}