all repos

anpi @ 1a0c16f82982b3a196c057da7e85f8279a52cd51

yaml to anki importer
5 files changed, 23 insertions(+), 18 deletions(-)
fix linter errors
Author: Oleksandr Smirnov olexsmir@gmail.com
Committed at: 2025-04-30 18:33:00 +0300
Parent: 2ef9e52
M .golangci.yml
···
        60
        60
             - rowserrcheck

      
        61
        61
             - sloglint

      
        62
        62
             - staticcheck

      
        63
        
        -    - tagalign

      
        64
        63
             - testableexamples

      
        65
        64
             - testifylint

      
        66
        65
             - testpackage

      ···
        85
        84
             gocyclo:

      
        86
        85
               min-complexity: 15

      
        87
        86
             govet:

      
        
        87
        +      disable:

      
        
        88
        +        - fieldalignment

      
        88
        89
               enable-all: true

      
        89
        90
             interfacebloat:

      
        90
        91
               max: 3

      
M anki/anki.go
···
        48
        48
         	}

      
        49
        49
         )

      
        50
        50
         

      
        51
        
        -func (a *AnkiClient) AddNote(

      
        52
        
        -	deckName, modelName string,

      
        53
        
        -	fields map[string]string,

      
        54
        
        -	tags []string,

      
        55
        
        -) (NoteID, error) {

      
        
        51
        +func (a *AnkiClient) AddNote(inp Note) (NoteID, error) {

      
        56
        52
         	res, err := request[NoteID]("addNote", internalNote{Note{

      
        57
        
        -		DeckName:  deckName,

      
        58
        
        -		ModelName: modelName,

      
        59
        
        -		Fields:    fields,

      
        60
        
        -		Tags:      tags,

      
        
        53
        +		DeckName:  inp.DeckName,

      
        
        54
        +		ModelName: inp.ModelName,

      
        
        55
        +		Fields:    inp.Fields,

      
        
        56
        +		Tags:      inp.Tags,

      
        61
        57
         	}})

      
        62
        58
         	if err != nil {

      
        63
        59
         		return 0, err

      
M anki/http.go
···
        19
        19
         

      
        20
        20
         func (r AnkiResponse[T]) CheckErrors() error {

      
        21
        21
         	if r.Error != "" {

      
        
        22
        +		//nolint:err113 // there's no way to parse those errors so i just return them as they come

      
        22
        23
         		return fmt.Errorf("%s", r.Error)

      
        23
        24
         	}

      
        24
        25
         	return nil

      ···
        42
        43
         		return AnkiResponse[R]{}, err

      
        43
        44
         	}

      
        44
        45
         

      
        
        46
        +	//nolint:noctx // there's no need for time out or anything like that at the moment

      
        45
        47
         	req, err := http.NewRequest(http.MethodGet, ankiURL, bytes.NewBuffer(bodyReq))

      
        46
        48
         	if err != nil {

      
        47
        49
         		return AnkiResponse[R]{}, err

      ···
        55
        57
         		return AnkiResponse[R]{}, err

      
        56
        58
         	}

      
        57
        59
         

      
        58
        
        -	defer resp.Body.Close() //nolint

      
        
        60
        +	defer resp.Body.Close()

      
        59
        61
         

      
        60
        62
         	var ankiResp AnkiResponse[R]

      
        61
        63
         	if err := json.NewDecoder(resp.Body).Decode(&ankiResp); err != nil {

      
M main.go
···
        29
        29
         }

      
        30
        30
         

      
        31
        31
         func run(fpath string) error {

      
        32
        
        -	anki := anki.NewAnkiClient()

      
        
        32
        +	ac := anki.NewAnkiClient()

      
        33
        33
         

      
        34
        34
         	f, err := os.ReadFile(filepath.Clean(fpath))

      
        35
        35
         	if err != nil {

      ···
        51
        51
         			slog.Info("gotten fields", "fields", fields)

      
        52
        52
         

      
        53
        53
         			tags := mergeTags(deck.Tags, note.Tags)

      
        54
        
        -			nid, err := anki.AddNote(deck.Deck, deck.Type, fields, tags)

      
        
        54
        +			nid, err := ac.AddNote(anki.Note{

      
        
        55
        +				DeckName:  deck.Deck,

      
        
        56
        +				ModelName: deck.Type,

      
        
        57
        +				Fields:    fields,

      
        
        58
        +				Tags:      tags,

      
        
        59
        +			})

      
        55
        60
         			if err != nil {

      
        56
        61
         				return err

      
        57
        62
         			}

      
M parser/parser.go
···
        1
        1
         package parser

      
        2
        2
         

      
        3
        3
         import (

      
        4
        
        -	"fmt"

      
        
        4
        +	"errors"

      
        
        5
        +	"maps"

      
        5
        6
         

      
        6
        7
         	"gopkg.in/yaml.v3"

      
        7
        8
         )

      ···
        30
        31
         

      
        31
        32
         func (d *DeckImport) buildLookUpTable() {

      
        32
        33
         	d.fieldLookUp = make(map[string]string)

      
        33
        
        -	for k, v := range d.Fields {

      
        34
        
        -		d.fieldLookUp[k] = v

      
        35
        
        -	}

      
        
        34
        +	maps.Copy(d.Fields, d.fieldLookUp)

      
        36
        35
         }

      
        37
        36
         

      
        38
        37
         type Note struct {

      ···
        62
        61
         	return nil

      
        63
        62
         }

      
        64
        63
         

      
        
        64
        +var ErrInvalidFileFormat = errors.New("invalid file format")

      
        
        65
        +

      
        65
        66
         func Parse(inp []byte) ([]DeckImport, error) {

      
        66
        67
         	var listRes []DeckImport

      
        67
        68
         	if err := yaml.Unmarshal(inp, &listRes); err == nil {

      ···
        77
        78
         		return []DeckImport{single}, nil

      
        78
        79
         	}

      
        79
        80
         

      
        80
        
        -	return nil, fmt.Errorf("invalid file format")

      
        
        81
        +	return nil, ErrInvalidFileFormat

      
        81
        82
         }