File: autorestMetadata.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (80 lines) | stat: -rw-r--r-- 2,203 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package autorest

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"

	"github.com/Azure/azure-sdk-for-go/eng/tools/generator/autorest/model"
)

// MetadataProcessError ...
type MetadataProcessError struct {
	MetadataLocation string
	Errors           []error
}

// Error ...
func (e *MetadataProcessError) Error() string {
	return fmt.Sprintf("total %d error(s) during processing metadata %s: %+v", len(e.Errors), e.MetadataLocation, e.Errors)
}

func (e *MetadataProcessError) add(err error) {
	e.Errors = append(e.Errors, err)
}

// MetadataProcessor processes the metadata
type MetadataProcessor struct {
	metadataOutputFolder string
}

// NewMetadataProcessorFromLocation creates a new MetadataProcessor using the metadata output folder location
func NewMetadataProcessorFromLocation(metadataOutput string) *MetadataProcessor {
	return &MetadataProcessor{
		metadataOutputFolder: metadataOutput,
	}
}

// Process returns the metadata result: a map from tag to Metadata, and an error if there is anything that could not be processed.
// the error returned must be of type *MetadataProcessError
func (p MetadataProcessor) Process() (map[string]model.Metadata, error) {
	fi, err := ioutil.ReadDir(p.metadataOutputFolder)
	if err != nil {
		return nil, &MetadataProcessError{
			MetadataLocation: p.metadataOutputFolder,
			Errors:           []error{err},
		}
	}
	result := make(map[string]model.Metadata)
	metadataErr := &MetadataProcessError{
		MetadataLocation: p.metadataOutputFolder,
	}
	for _, f := range fi {
		// a metadata output must be a json file
		if f.IsDir() || !strings.HasSuffix(f.Name(), ".json") {
			continue
		}
		file, err := os.Open(filepath.Join(p.metadataOutputFolder, f.Name()))
		if err != nil {
			metadataErr.add(err)
			continue
		}
		metadata, err := model.NewMetadataFrom(file)
		if err != nil {
			metadataErr.add(err)
			continue
		}
		tag := strings.TrimSuffix(f.Name(), ".json")
		result[tag] = metadata
	}

	if len(metadataErr.Errors) != 0 {
		return result, metadataErr
	}
	return result, nil
}