+func loadTypesPackages(types ...string) ([]*context, error) {
+ var ctxs []*context
+ for _, inputFile := range types {
+ // create generator context
+ ctx, err := newContext(inputFile, "")
+ if err != nil {
+ return nil, err
+ }
+ // read API definition from input file
+ ctx.inputData, err = ioutil.ReadFile(ctx.inputFile)
+ if err != nil {
+ return nil, fmt.Errorf("reading input file %s failed: %v", ctx.inputFile, err)
+ }
+ // parse JSON data into objects
+ jsonRoot, err := parseInputJSON(ctx.inputData)
+ if err != nil {
+ return nil, fmt.Errorf("parsing JSON input failed: %v", err)
+ }
+ ctx.packageData, err = parsePackage(ctx, jsonRoot)
+ if err != nil {
+ return nil, fmt.Errorf("parsing package %s failed: %v", ctx.packageName, err)
+ }
+ ctxs = append(ctxs, ctx)
+ }
+ return ctxs, nil
+}
+
+func loadTypeAliases(ctx *context, typesCtxs []*context) error {
+ for _, t := range ctx.packageData.Types {
+ for _, c := range typesCtxs {
+ if _, ok := ctx.packageData.Imports[t.Name]; ok {
+ break
+ }
+ for _, at := range c.packageData.Types {
+ if at.Name != t.Name {
+ continue
+ }
+ if len(at.Fields) != len(t.Fields) {
+ continue
+ }
+ ctx.packageData.Imports[t.Name] = Import{
+ Package: c.packageName,
+ }
+ }
+ }
+ }
+ for _, t := range ctx.packageData.Aliases {
+ for _, c := range typesCtxs {
+ if _, ok := ctx.packageData.Imports[t.Name]; ok {
+ break
+ }
+ for _, at := range c.packageData.Aliases {
+ if at.Name != t.Name {
+ continue
+ }
+ if at.Length != t.Length {
+ continue
+ }
+ if at.Type != t.Type {
+ continue
+ }
+ ctx.packageData.Imports[t.Name] = Import{
+ Package: c.packageName,
+ }
+ }
+ }
+ }
+ for _, t := range ctx.packageData.Enums {
+ for _, c := range typesCtxs {
+ if _, ok := ctx.packageData.Imports[t.Name]; ok {
+ break
+ }
+ for _, at := range c.packageData.Enums {
+ if at.Name != t.Name {
+ continue
+ }
+ if at.Type != t.Type {
+ continue
+ }
+ ctx.packageData.Imports[t.Name] = Import{
+ Package: c.packageName,
+ }
+ }
+ }
+ }
+ for _, t := range ctx.packageData.Unions {
+ for _, c := range typesCtxs {
+ if _, ok := ctx.packageData.Imports[t.Name]; ok {
+ break
+ }
+ for _, at := range c.packageData.Unions {
+ if at.Name != t.Name {
+ continue
+ }
+ ctx.packageData.Imports[t.Name] = Import{
+ Package: c.packageName,
+ }
+ }
+ }
+ }
+ return nil
+}
+