forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
options.go
71 lines (62 loc) · 1.58 KB
/
options.go
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
package model
import (
"encoding/json"
"io"
"io/ioutil"
)
// Options ...
type Options interface {
// Arguments returns the argument defined in this options
Arguments() []Option
// CodeGeneratorVersion returns the code generator version defined in this options
CodeGeneratorVersion() string
}
type generateOptions struct {
AutorestArguments []string `json:"autorestArguments"`
}
type localOptions struct {
arguments []Option
codeGenVersion string
}
// NewOptionsFrom returns a new instance of Options from the given io.Reader
func NewOptionsFrom(reader io.Reader) (Options, error) {
b, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
var raw generateOptions
if err := json.Unmarshal(b, &raw); err != nil {
return nil, err
}
var options []Option
var codeGenVersion string
for _, r := range raw.AutorestArguments {
o, err := NewOption(r)
if err != nil {
return nil, err
}
options = append(options, o)
if v, ok := o.(KeyValueOption); ok && v.Key() == "use" {
codeGenVersion = v.Value()
}
}
return &localOptions{
arguments: options,
codeGenVersion: codeGenVersion,
}, nil
}
// Arguments ...
func (o localOptions) Arguments() []Option {
return o.arguments
}
// CodeGeneratorVersion ...
func (o localOptions) CodeGeneratorVersion() string {
return o.codeGenVersion
}
// String ...
func (o localOptions) String() string {
b, _ := json.MarshalIndent(o, "", " ")
return string(b)
}